最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Best way to set timer in ReactRedux for sending refresh auth token - Stack Overflow

programmeradmin1浏览0评论

I'm using the access refresh jwt authentication flow and want the client to send a refresh token to get a new access token every 10 minutes after it received the access token. I also want to make sure that if the user closes their laptop for an hour and es back to it that a new access token request is also sent. What is the best way to implement this behavior in React/redux, where the user will always have a valid access token and seamlessly keeps their "session" going?

I'm using the access refresh jwt authentication flow and want the client to send a refresh token to get a new access token every 10 minutes after it received the access token. I also want to make sure that if the user closes their laptop for an hour and es back to it that a new access token request is also sent. What is the best way to implement this behavior in React/redux, where the user will always have a valid access token and seamlessly keeps their "session" going?

Share Improve this question asked Feb 19, 2018 at 6:49 PurplePandaPurplePanda 68310 silver badges30 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

To achieve that you can conditionnally check the expiration of the token before each request instead of setting a timer.

This will depend on the way you municate with the server but the idea is to store client side the jwt token, his expiration and the refresh token (and his expiration too if needed) then use some sort of middleware before each request that need the auth :

const authClientMiddleware = (done) => {
    if (new Date(localStorage.getItem('expiration')) <= new Date()) {
        getNewToken(localStorage.getItem('refreshToken')).then(() => done()).catch(() => logout());
    } else {
        done();
    }
}
发布评论

评论列表(0)

  1. 暂无评论