我有一个应用程序,它将使用cognito作为身份验证提供程序。我注意到id and access token都在一个小时后过期。这似乎并不长。
我已经想到了两种管理令牌的方法,但是不确定选择哪种/最佳实践。
在对后端的每次请求之前,我可以检查令牌的到期时间,如果有效,请使用令牌,如果无效,则可以使用刷新令牌获取新令牌并使用它。
或
我可以在每个请求中刷新令牌,并对请求使用新的ID /访问令牌。
大多数人如何管理这些短暂的令牌?
我有一个应用程序,它将使用cognito作为身份验证提供程序。我注意到id and access token都在一个小时后过期。这似乎并不长。
我已经想到了两种管理令牌的方法,但是不确定选择哪种/最佳实践。
在对后端的每次请求之前,我可以检查令牌的到期时间,如果有效,请使用令牌,如果无效,则可以使用刷新令牌获取新令牌并使用它。
或
我可以在每个请求中刷新令牌,并对请求使用新的ID /访问令牌。
大多数人如何管理这些短暂的令牌?
使用cognito,您将获得3种令牌,所有令牌都存储在存储器中。 1)访问令牌。 (有效期为1小时) 2)ID-令牌。 (有效期为1小时) 3)刷新令牌。 (有效期为1个月或2个月,请验证)
//对于Web App
我已将AWS-Amplify用于我的Web客户端。
在目标网页中,您应该调用Auth.currentSeesion();
如果访问令牌和ID令牌的寿命(1小时)被暴露,这将被调用,则它将寻找刷新令牌,然后aws放大将带回访问令牌和ID令牌并存储到存储中。
>链接:https://aws-amplify.github.io/docs/js/authentication#token-refresh
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.1/umd/react-dom.production.min.js"></script>
useEffect(() => {
analytics.initialize();
// Update Authorization token
auth.currentSession()
.then(data => {
storage.saveDataInLocal(StorageKey.ACCESS_TOKEN,data.idToken.jwtToken);
const { email } = data.idToken.payload;
// checking if user was signed in by gmail
if(props.isGoogleSigned) {
// this code is required to auto login user if he closed the tab last time when he was logined
props.dispatch(sendGoogleSignInRequest(email));
}
props.dispatch(initialCall());
})
.catch(() => {
props.dispatch(initialCall());
});
// Validate user authentication status
auth.currentAuthenticatedUser()
.then(() => {
props.dispatch(fetchAppData());
}
)
.catch(() => {
if (storage.getDataFromLocal(StorageKey.USER_INFO)) {
props.dispatch(clearAppReducer());
storage.clearAllLocalData();
storage.clearAllSessionData();
props.history.push('/login');
}
});
},[]);