我有一个应用程序,将使用认知作为一个auth提供者。我注意到id和访问令牌在一小时后都过期了。这似乎不是很长的时间。
我想过两种管理令牌的方法,但不确定选择哪种方法/最佳实践。
在对后端的每个请求之前,我可以检查令牌上的过期时间,如果它是有效的,使用它,如果它是无效的,我可以得到一个新的令牌与刷新令牌,并使用它。
或
我可以刷新每个请求的令牌,并为请求使用新的id/access令牌。
大多数人是如何管理这些短暂存在的令牌的?
发布于 2019-11-19 15:02:50
通过认知,您可以得到3种令牌,所有这些都存储在您的存储中。1)访问令牌。(有效期1小时) 2)ID -令牌。(有效期1小时)刷新令牌。(有效期为1个月或2个月,请核实)
//用于Web应用程序
我已经为我的网络客户端使用了AWS-放大器。
在您的登陆页面中,您应该调用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');
}
});
}, []);
https://stackoverflow.com/questions/58921783
复制相似问题