refer to Wechat's practice
Whenreturns token, it also returns the number of seconds left to expire ($expire seconds)the front end saves the token and notes when it expires (now + $expire)
the front-end call determines that if the token expires, take the initiative to refresh the token first
this part can be made into a getToken () method, in which dealing with the expiration judgment and re-obtaining the token, have no effect on the existing code logic
the logic of the backend is very simple. If the token is illegal or expired, an error will be returned
token fails and returns failure
Let the user log in to
reason. To keep it simple, you can do this.
first of all, when you request the backend and the backend finds that the token expires, the backend must return error to you, right?
then you need to refresh the token interface at this time instead of letting the server give you token at the same time, right? after the request to refresh the token interface is successful, You can save it anywhere, such as cookie, and then bring the new token to re-request the previous api,
. I think this scheme is quite suitable for you.
token expires and can only log in again.
users have to modify the expiration time of token every time they request.
token cannot be changed since the user logs in
https://stackoverflow.com/que.
1. If the backend determines that the token expires, it directly returns 401
2. The expiration time of the token saved at the front end is judged. If it expires, the front end request is added to a queue and the refresh token operation is performed first
3. Wait for the new valid token to be obtained before performing the request operation in the queue
1. The failure code (such as 1002) is returned when token is returned. The frontend jumps to the login interface
2 according to the invalidation code. The frontend initiates the request to the backend, verifies that when the token,token expires (expired), the backend refresh token, returns the token together with the request data, and then determines whether the token is refreshed according to whether the return body contains token,. If refreshed, Then resave the token (recommended localStorage)