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

I'm using the access refresh jwt authentication flow and want the client to send a refresh token t

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();
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745288937a4620737.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信