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.This points to 1. Who calls whom? example: func...
1.1 Data Type Overview The data type is a field c...
Copy code The code is as follows: <div style=&...
Conclusion: In a multithreaded environment, if on...
html <div class="totop" v-show="...
Install Jenkins via Yum 1. Installation # yum sou...
This article shares the specific code of the firs...
Table of contents 1. What is a template string? 2...
1. I purchased a VPS and CentOS system, and found...
The ".zip" format is used to compress f...
The 10-day tutorial uses the most understandable ...
This article shares with you a detailed tutorial ...
need: Merge identical items of one field and sort...
I just joined a new company recently. After getti...
This article shares the specific code of JavaScri...