Preface: Recently, when I was working on a demand, it involved a login Frontend: Backend, can you set the token expiration time to be longer? Backend: Yes, but it is not safe to do so, you can use a better method. Front-end: What method? Backend: Provides an interface for refreshing tokens and refreshes tokens regularly Front-end: OK, let me think about it. 1. Demand When Method 1 The backend returns the expiration time, the frontend determines Disadvantages: The backend needs to provide an additional field for Method 2 Write a timer to refresh the Disadvantages: waste of resources, consumption of performance, not recommended. Method 3 Intercept in the response interceptor, determine that 2. Implementation The basic framework of import axios from 'axios' service.interceptors.response.use( response => { if (response.data.code === 409) { return refreshToken({ refreshToken: localStorage.getItem('refreshToken'), token: getToken() }).then(res => { const { token } = res.data setToken(token) response.headers.Authorization = `${token}` }).catch(err => { removeToken() router.push('/login') return Promise.reject(err) }) } return response && response.data }, (error) => { Message.error(error.response.data.msg) return Promise.reject(error) }) 3. Problem SolvingQuestion 1: How to prevent multiple token refreshes We use a variable import axios from 'axios' service.interceptors.response.use( response => { if (response.data.code === 409) { if (!isRefreshing) { isRefreshing = true return refreshToken({ refreshToken: localStorage.getItem('refreshToken'), token: getToken() }).then(res => { const { token } = res.data setToken(token) response.headers.Authorization = `${token}` }).catch(err => { removeToken() router.push('/login') return Promise.reject(err) }).finally(() => { isRefreshing = false }) } } return response && response.data }, (error) => { Message.error(error.response.data.msg) return Promise.reject(error) }) Question 2: When two or more requests are initiated at the same time, how do other interfaces solve this problem? When the second expired request comes in, Final code: import axios from 'axios' //Whether it is refreshing let isRefreshing = false //Retry queue let requests = [] service.interceptors.response.use( response => { //Agreed code 409 token expired if (response.data.code === 409) { if (!isRefreshing) { isRefreshing = true //Call refresh token interface return refreshToken({ refreshToken: localStorage.getItem('refreshToken'), token: getToken() }).then(res => { const { token } = res.data // Replace token setToken(token) response.headers.Authorization = `${token}` // After token is refreshed, re-execute the array method requests.forEach((cb) => cb(token)) requests = [] // Re-request and clear return service(response.config) }).catch(err => { //Jump to the login page removeToken() router.push('/login') return Promise.reject(err) }).finally(() => { isRefreshing = false }) } else { // Returns a Promise that has not been resolved return new Promise(resolve => { // Save resolve in function form and wait for refresh before executing requests.push(token => { response.headers.Authorization = `${token}` resolve(service(response.config)) }) }) } } return response && response.data }, (error) => { Message.error(error.response.data.msg) return Promise.reject(error) } ) This is the end of this article on how to implement seamless token refresh. For more information on implementing seamless token refresh, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Build a Docker image using Dockerfile
1. Set up Chinese input method 2. Set the double ...
1. Source of the problem A friend @水米田 asked me a...
Table of contents 1. MySQL data backup 1.1, mysql...
describe: fuser can show which program is current...
The reason why Docker is so popular nowadays is m...
Download and installConfigure environment variabl...
Preface After reading the previous article about ...
<br />First think of the idea, then draw a s...
Often, after a web design is completed, the desig...
Table of contents Typical Cases Appendix: Common ...
Table of contents 1. Background 2. Slow query cau...
Table of contents 1. What is a component? 2. Crea...
Table of contents introduce Support Intel CPU Sup...
XML files should be encoded in utf-8 as much as p...
Currently, most CPUs support floating-point units...