TargetHandling token expiration scenarios
Thought AnalysisBackend : When receiving a request from a user to access a certain interface, check whether the current token is invalid. If the token is invalid, return an agreed status code 10002 to the frontend. Front-end : In the response interceptor, analyze the return value of the interface. If the status code is 10002, perform token invalidation operation. Code landing In Since page jumps require routing, we first introduce // Import routing import router from '@/router' Code // In the response interceptor // 1. Determine whether the operation is successful based on the data returned by the backend, and report an error if it is unsuccessful // 2. If successful, only return valid data service.interceptors.response.use( response => { // Agreement between backend and frontend: success=true indicates successful request if (response.data.success) { return response.data } else { // If success is false, the business is wrong, and reject is triggered directly // Captured by catch branch return Promise.reject(new Error(response.data.message)) } }, async error => { console.log('Request error', error) if (error.response.data.code === 10002) { console.log('token expired') await store.dispatch('user/logout') // .vue -- this.$route.fullPath // .js -- router.currentRoute.fullPath router.push('/login?return_url=' + encodeURIComponent(router.currentRoute.fullPath)) } console.dir(error) return Promise.reject(error) } ) The above solution is the backend-led solution. The frontend only needs to get the error code to do business processing. This solution is also the most commonly used and safest solution. SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: 10 excellent Web UI libraries/frameworks
>>: The current better way to make select list all options when selected/focused
This article mainly introduces the case of Vue en...
Table of contents Installation package download I...
Problem Description html <iframe id="h5Co...
Table of contents A pitfall about fileReader File...
This article shares the specific code of typescri...
Container lifecycle The life cycle of a container...
Project requirements: When you click a product tr...
This article introduces the installation and use ...
Preface In the development process, defining vari...
To replace a string, we need to use the following...
In Windows operating system, the program to query...
mysqladmin is an official mysql client program th...
Table of contents 1. Achieve results 2. Implement...
This article uses examples to illustrate the usag...
1. Preliminary preparation (windows7+mysql-8.0.18...