首页 > 解决方案 > Aws 放大登录、注册和确认签名工作流程问题

问题描述

我已经在 Angular 项目中实现了 AWS-Amplify,它在注册、登录和注册后确认用户时运行良好,但是我面临流程问题,我正在尝试寻找它,我的问题是当用户注册时他会被重定向到确认页面输入确认码,提交代码后用户必须登录才能访问我的网站,所以我尝试在成功后直接登录用户注册并确认帐户,但无法解决,这是我的注册代码:

onSubmitSignup(value: any) {
    const email = value.email, password = value.password;
    let attributes: {
        username,
        phone_number,
    };
    this.auth.signUp(email, password, attributes)
        .subscribe(
            result => {
                this.successfullySignup = true;
                this.router.navigate(['/pages/confirmRegistration', result.user.username]);
            },
            error => {
                console.log(error);
                this.errorMessage = error.message;
            });
}

在我的身份验证服务中:

public signUp(email, password, userAttributes): Observable<any> {
    return fromPromise(Auth.signUp(email, password, userAttributes));
}

这是确认用户的代码:

onSubmitConfirmation(value: any) {
    const email = this.email, confirmationCode = value.confirmationCode;
    this.auth.confirmSignUp(email, confirmationCode)
        .subscribe(
            result => {
                this.auth.signIn(this.email, this.password); //this what im trying to achieve is to have the user data from the sign in and sign up components here in the confirmation component 
            },
            error => {
                console.log(error);
                this.errorMessage = error.message;
            });
}

以及为此提供的服务:

public confirmSignUp(email, code): Observable<any> {
    return fromPromise(Auth.confirmSignUp(email, code));
}

我的登录方法:

onSubmitLogin(value: any) {
  const email = value.email, password = value.password;
  this.auth.signIn(email, password)
    .subscribe(
      result => {
        this.router.navigate(['']);
      },
      error => {
        this.errorMessage = error.message;
        if (error.code === 'UserNotConfirmedException') {
          this.router.navigate(['/pages/confirmRegistration']);
        }
      });
}

以及为此提供的服务:

public signIn(email, password): Observable<any> {
    return fromPromise(Auth.signIn(email, password))
        .pipe(
            tap(() => this.loggedIn.next(true))
        );
}

我虽然关于将用户名和密码作为我所有身份验证组件之间的共享数据然后使用它们,但认为这不是一个好习惯并且没有设法实现它,所以如果你能提出更好的方法来做到这一点,你可以建议吗?我想通过电子邮件和密码使用:

this.router.navigate(['/pages/confirmRegistration', result.user.username]);

但这会暴露 url 中的密码,我认为这不是一个好主意,我不是专家,但如果是请建议。

我什至考虑在每个 signIn 和 signUp 的相同组件中使用确认方法,但这会重复代码,但在确认用户后我可能能够访问登录所需的字段。

标签: angularamazon-cognitoaws-amplify

解决方案


推荐阅读