【问题标题】:Persist AWS Cognito User with Amplify使用 Amplify 持久化 AWS Cognito 用户
【发布时间】:2019-04-28 17:31:55
【问题描述】:

我一直在关注Serverless Stack tutorial,并且可以通过拨打Auth.signIn(username, passsword)得到积极的回应

我们目前的工作流程是用户需要重置密码,因为帐户将被分发。

.changePassword 函数有 3 个参数;用户、旧密码、新密码

我终其一生都无法弄清楚它在为用户寻找什么。当我设置从.signIn() 返回的对象时,出现以下错误:

本地存储缺少 ID 令牌,请进行身份验证

显然我不会将此流程用于生产,但我正在尝试弄清楚 Auth 正在寻找什么。

Auth.signIn(this.state.emailAddress, this.state.password)
  .then(user => {
    this.setState({ isLoading: false, user });
  }).then(async () => {
    Auth.changePassword(this.state.user, 'P@ssw0rd!', 'NewP@ssw0rd!');
  }).catch(err => {
    this.setState({ isLoading: false, errorMessage: err.message })
});

我确实在 .signIn 返回的对象的 Storage 属性中看到了一个 ID 令牌。澄清一下:我可能不应该把它放在链接中。在实践中我并没有真正做到以上。当我保存来自 Signin 的响应并将其传递给 changePassword 时,我收到本地存储错误。我想知道设置 Amplify 是否存在配置问题,通常会将此信息放在 localStorage 中。

【问题讨论】:

    标签: reactjs amazon-web-services amazon-cognito aws-amplify amplifyjs


    【解决方案1】:

    Auth.changePassword 接受CognitoUser 作为它的第一个参数,这是should be returned 来自Auth.signIn

    这里的问题是你的承诺链,并使用this.setState() 并在实际设置之前读回它:

    Auth.signIn(this.state.emailAddress, this.state.password)
      .then(user => {
        // Triggers a setstate here:
        this.setState({ isLoading: false, user });
      }).then(async () => {
        // this.state.user is not defined at this point
        Auth.changePassword(this.state.user, 'P@ssw0rd!', 'NewP@ssw0rd!');
      }).catch(err => {
        this.setState({ isLoading: false, errorMessage: err.message })
    });
    

    来自React docs

    setState() 并不总是立即更新组件。它可能会批量更新或将更新推迟到以后。这使得在调用 setState() 之后立即阅读 this.state 是一个潜在的陷阱。相反,请使用componentDidUpdatesetState 回调(setState(updater, callback)),保证在应用更新后触发其中任何一个。如果您需要根据之前的状态设置状态,请阅读下面的 updater 参数。

    解决此问题的最简单方法是在第一个 .then 回调中返回用户,然后将其传递给第二个:

    Auth.signIn(this.state.emailAddress, this.state.password)
      .then(user => {
        // Triggers a setstate here:
        this.setState({ isLoading: false, user });
        return user;
      }).then((user) => {
        // this.state.user is not defined at this point
        Auth.changePassword(user, 'P@ssw0rd!', 'NewP@ssw0rd!');
      }).catch(err => {
        this.setState({ isLoading: false, errorMessage: err.message })
    });
    

    就个人而言,我认为完全在 async/await 中会更好看:

    try {
        const user = await Auth.signIn(this.state.emailAddress, this.state.password);
    
        this.setState({ isLoading: false, user });
    
        await Auth.changePassword(user, 'P@ssw0rd!', 'NewP@ssw0rd!');
    } catch (err) {
        this.setState({ isLoading: false, errorMessage: err.message })
    }
    

    【讨论】:

    • 我可能不应该将它放在链接中。在实践中我并没有真正这样做。当我保存来自 Signin 的响应并将其传递给 changePassword 时,我收到本地存储错误。我完全同意这应该是从 Signin 返回的内容,这就是我感到困惑的原因。我想知道设置 Amplify 是否存在配置问题,通常会将这些信息放在 localStorage 中
    • 看看this issue @JonHarding,会不会是“你们需要使用completeNewPassword 方法而不是changePassword。这实际上将完成API 提出的身份验证挑战。”?
    • 我已经有一段时间无法重温这个了。你得到赏金积分了吗?
    • 不,今天早上到期,没有奖金@JonHarding
    猜你喜欢
    • 2018-08-03
    • 2019-02-08
    • 2019-07-31
    • 2019-03-04
    • 2019-10-11
    • 2019-09-08
    • 2019-06-02
    • 2020-01-01
    • 2020-05-10
    相关资源
    最近更新 更多