【问题标题】:Integration with Amplify JS and Cognito in React在 React 中与 Amplify JS 和 Cognito 集成
【发布时间】:2019-11-06 22:16:26
【问题描述】:

我一直在尝试使用 Amplify JS 进行身份验证,并且能够在 user.challengeName === 'NEW_PASSWORD_REQUIRED' 时登录和更改密码,并且我的 Approuter 中有 componentDidMount(),其中所有路由站点。当导航到更改密码屏幕时componentDidMount() 没有调用。

登录条件:

 if (user.challengeName === 'NEW_PASSWORD_REQUIRED') {

     this.props.history.push('/ChangePassword')}

在 Approuter 中:

async componentDidMount() {

    try {
      const session = await Auth.currentSession();
      this.setAuthStatus(true);
      console.log(session);
      const user = await Auth.currentAuthenticatedUser();debugger;
      this.setUser(user);
    } catch(error) {
      if (error !== 'No current user') {
        console.log(error);
      }
    }

更改密码:

  let user =  Auth.currentAuthenticatedUser();

  Auth.changePassword(
        user, 
        values.tempPassword,              
        values.newPassword

      ).then(user => {
        // at this time the user is logged in if no MFA required
        console.log(user);
      }).catch(e => {
        console.log(e);
      });

预期:

  1. 用户必须是 Cognito 对象,更改密码应更新并确认用户。

实际:

User.session() 不是函数。

【问题讨论】:

标签: javascript reactjs authentication amazon-cognito


【解决方案1】:

我在 Angular 中有一个示例(可能与 React 的代码相同):

cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: result => {
        console.log(err);
        ...
    },
    onFailure: err => {
      console.log(err);
      ...
    },
    newPasswordRequired: (userAttributes, requiredAttributes) => {
      subscriber.error({
        reason: 'FORCE_CHANGE_PASSWORD',
        user: {
          username: user
        }
      });
    }
  });

【讨论】:

  • 我目前正在使用 Amplify JS 进行身份验证过程。
  • @HemanthBatchu,你的 Amplify JS 不是 amazon-cognito-identity-js
  • @HemanthBatchu,如果你使用aws-amplify-react,下面是他们测试这个特殊情况的方法:github.com/aws-amplify/amplify-js/blob/master/packages/…
猜你喜欢
  • 2021-07-31
  • 2021-03-01
  • 2019-03-04
  • 2019-05-19
  • 1970-01-01
  • 2021-11-20
  • 2019-12-25
  • 1970-01-01
  • 2020-08-16
相关资源
最近更新 更多