【问题标题】:How can I force a cognito token refresh from the client如何从客户端强制刷新 cognito 令牌
【发布时间】:2018-06-28 20:19:05
【问题描述】:

我正在使用 aws amplify,并且我知道令牌会在需要时自动刷新,并且这是在幕后完成的。

我需要做的是通过 Lambda 后端进程更改 cognito 用户池中用户的自定义属性。这是我能做到的,而且它正在工作。但是,Web 客户端用户永远不会看到这个新的自定义属性,我认为他们可以看到它的唯一方法是刷新令牌,因为值存储在 JWT 令牌中。

【问题讨论】:

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


【解决方案1】:

就像这里所说的:

https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html

访问令牌和 ID 令牌有效期为 1 小时。使用 Amplify,您可以在 Auth 类中使用 currentSessioncurrentUserInfo 获取有关会话的信息,以便能够检索有关令牌的信息。

【讨论】:

    【解决方案2】:

    未记录,但您可以在用户上使用refreshSession 方法。您对 currentAuthenticatedUser 和 currentSession 的下一次调用将更新配置文件属性(和组)

    User = Auth.currentAuthenticatedUser()
    Session =  Auth.currentSession()
    
    User.refreshSession(Session.refreshToken)
    

    【讨论】:

    • 你知道哪个版本的 Amplify 有这个吗?在我使用的版本中,我得到User.refreshSession is not a function
    【解决方案3】:

    以下是按需更新令牌的方法(强制)

    import { Auth } from 'aws-amplify';
    
    try {
      const cognitoUser = await Auth.currentAuthenticatedUser();
      const currentSession = await Auth.currentSession();
      cognitoUser.refreshSession(currentSession.refreshToken, (err, session) => {
        console.log('session', err, session);
        const { idToken, refreshToken, accessToken } = session;
        // do whatever you want to do now :)
      });
    } catch (e) {
      console.log('Unable to refresh Token', e);
    }

    【讨论】:

      【解决方案4】:

      截至 2021 年的正确解决方案是致电 await Auth.currentAuthenticatedUser({bypassCache: true})

      【讨论】:

        猜你喜欢
        • 2019-04-23
        • 1970-01-01
        • 2016-09-23
        • 2018-12-08
        • 2019-12-30
        • 2013-12-05
        • 2020-08-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多