【发布时间】: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