【问题标题】:How to refresh token using AWS coginto auth sdk如何使用 AWS cognito auth sdk 刷新令牌
【发布时间】:2021-03-20 06:44:06
【问题描述】:

我正在开发令牌过期后刷新的功能。我使用 amazon-cognito-auth-js 进行授权并检查 here 作为示例,我实现了以下方法来刷新令牌。但是它不起作用。 下面是我的代码,会话没有像我预期的那样刷新。没有语法错误,只是身份验证令牌仍然过期。 顺便说一句,我用的是 react。

import { CognitoAuth } from 'amazon-cognito-auth-js';
class Main extends Component {
   constructor() {
        this.state = {
            auth: ""
       }
   }
   componentDidMount() {
        //some logic to get the auth once user login success
        //here is the logic  to update the correct auth into the state
        this.setState({
            auth: auth
        })
    }
 //here is the method that check the token expire or not, if expire, refresh the token and update the state
   checkTokenExpiration (){
        let auth = this.state.auth;
        let user = auth.getCachedSession();
 //ideally, there shall have the logic to check the session is expired or not
// anyidea how to write it?
        auth.refreshSession(user.getRefreshToken().getToken());
        this.setState({
            auth:auth
        })
}

}

【问题讨论】:

  • 你不需要做任何事情!如果您使用 cognito SDK 进行身份验证,SDK 将为您刷新令牌,无需代码。如果您有关于令牌到期的特定问题,您可能需要提出不同的问题。
  • @F_SO_K 我找到了解决方案。会话到期后,只需再次登录,它就会给我令牌。

标签: reactjs amazon-cognito access-token


【解决方案1】:

我自己结束了这个问题。 我的解决方案是在前端设置一个计时器,一旦计时器超过 1 小时(例如)。只是让用户注销。 Here 是您不应在 SPA 中刷新令牌的原因。 而here是处理refresh_token expires的解决方案

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 2023-03-16
    • 2022-01-02
    • 2017-11-22
    • 1970-01-01
    • 2018-04-03
    • 2020-09-28
    • 2020-08-30
    • 2016-09-23
    相关资源
    最近更新 更多