【问题标题】:How to reload localStorage saved aws-amplify auth?如何重新加载 localStorage 保存的 aws-amplify 身份验证?
【发布时间】:2021-09-14 15:14:21
【问题描述】:

我已成功使用 AWS Amplify 和 AWS Cognito 进行水疗中心授权。 唯一的问题是,当我刷新页面时,身份验证似乎丢失了,即使我在 LocalStorage 中看到了放大和认知项目。

这是我的配置部分:

Amplify.configure({
  Auth: {
    oauth: {
      domain: 'xxxxx.auth.eu-west-1.amazoncognito.com',
      scope: ['phone', 'email', 'profile', 'openid'],
      responseType: 'code',
      oauth.redirectSignIn: 'http://localhost:3000/',
      oauth.redirectSignOut: 'http://localhost:3000/',
    },
    region: process.env.REACT_APP_REGION,
    userPoolId: process.env.REACT_APP_USER_POOL_ID,
    userPoolWebClientId: process.env.REACT_APP_USER_POOL_WEB_CLIENT_ID,
    storage: localStorage,
  },
});

实际上,当用户通过身份验证并且应用重新启动(页面重新加载)时,我不知道如何让 amplify 使用 localStorage。
可能我应该使用 Auth.currentAuthenticatedUser(),但我想我不能调用它而不是 Auth.configure()...

【问题讨论】:

  • 您的代码 sn-p 不是有效的 javascript...您的对象声明中间有属性分配。您还使用了全局localStorage,而是通过window.localStorage。或者删除它,因为它无论如何都是默认的。在任何情况下调用 currentAuthenticatedUser 都应该从存储中重新加载用户。
  • @AndrewGillis:是的。刚刚更正了我的代码,错误是由于从我的代码中快速复制'n'paste以简化问题......关于localStorage,它也可以在没有window.的情况下工作,不知道为什么......但是@ 987654325@ 是正确答案...昨天我自己也来了。我想自己回答我的问题。如果你想回答,我会接受...
  • 是的,我知道您可以传递localStorage,但是它没有在范围内声明并且附加到窗口对象,因此您应该明确引用它。此外,代码仍然不正确。删除oauth.

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


【解决方案1】:

您可以通过调用Auth.currentAuthenticatedUser()从存储中获取用户。

它没有正确记录,但您可以refer to the implementation 查看行为(放大团队必须相信自记录代码)。

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 1970-01-01
    • 2021-02-24
    • 2020-09-08
    • 2019-08-10
    • 2019-06-12
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多