Why does Auth.signIn() always return 'undefined'?

0

I have a user pool with two users in it. One has been registered using the Auth APIs in an Angular app with email confirmation. The other one has been configured externally with confirmation status 'Force change password'. The temporary password was sent in an email.

In my Angular app when I try to call Auth.signIn() for either user, both succeed but the Promise returned by Auth.signIn() is always undefined. The code snippet is below:

 login(): void
  {
    console.log(this.user);
    this.cognitoService.signIn(this.user).then((cognitoUser) =>
    {
      this.cognitoUser = cognitoUser;
      console.log("CognitoUser obtained from Auth.signIn(): " + this.cognitoUser);
      if (typeof this.cognitoUser !== "undefined" && typeof this.cognitoUser.challengeName !== "undefined")
      {
        this.handleCognitoUser();
      }
      else
      {
        sessionStorage.setItem('state', 'loggedin');
        console.log('state: ' + sessionStorage.getItem('state'));
        this.cognitoService.setUpCurrentSession();
        this.router.navigate(['home']);
      }
    }).catch((err) =>{

      console.log("Got error code " + err.code);


      if (err.code === 'UserNotConfirmedException') {
        // The error happens if the user didn't finish the confirmation step when signing up
        // In this case you need to resend the code and confirm the user
        // We navigate to redo-confirmation, invoke the resendSignUp method which will
        // cause another email to be sent with the confirmation code
        sessionStorage.setItem('state', 'registered');
        console.log('state: ' + sessionStorage.getItem('state'));
        console.log('User registered but not confirmed');
        this.router.navigate(['redo-confirmation'],
          {queryParams: {'email': this.user.email}});
      }
      else if (err.code === 'PasswordResetRequiredException') {
        this.toastr.warning('Your Password has been adminstratively reset', 'Invoke "forgot or change password" to continue');
        // The error happens when the password is reset in the Cognito console
        // In this case you need to call forgotPassword to reset the password
        // Please check the Forgot Password part.
      }
      else if (err.code === 'NotAuthorizedException') {
        this.toastr.warning('Invalid credentials', '... or unregistered?');
        // The error happens when the incorrect password is provided
      }
      else if (err.code === 'InvalidParameterException') {
        this.toastr.warning('Invalid entry');
        // The error happens when the incorrect password is provided
      }
      else if (err.code === 'UserNotFoundException') {
        this.toastr.warning('User not found', 'Do you need to register?');
        // The error happens when the supplied username/email does not exist in the Cognito user pool
      }
      else {
        console.log(err);
      }

    });
  }

When I print out the returned value of the promise, it is always undefined, so I never get the option to call handleCognitoUser(). That is where I would handle the case like this.cognitoUser.challengeName === 'NEW_PASSWORD_REQUIRED' . The documentation I have read here https://docs.amplify.aws/lib/auth/manageusers/q/platform/js/#complete-new-password clearly shows that the response should not be undefined. What could be going wrong? What am I missing? Thanks

brianR
已提问 1 年前58 查看次数
没有答案

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容