Detailed explanation of VUE Token's invalidation process

Detailed explanation of VUE Token's invalidation process

Target

Handling token expiration scenarios

Token, as the key token information of the user, is not valid for a long time. Generally, there will be an expiration time (determined by the backend after what time it will expire). If the expiration time is exceeded, the current token can no longer be used as a user identification to request data. At this time, we need to do some additional expiration processing

Thought Analysis

insert image description here

Backend : 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 **src/utils/request.js** , add custom logic when handling the error of the response interceptor

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.

Summarize

This 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:
  • Vue implements automatic jump to login page when token expires
  • Vue keeps the user logged in (various token storage methods)
  • Token verification login in Vue project (front-end part)
  • Vue implements user login and token verification
  • VUE implements token login verification

<<:  10 excellent Web UI libraries/frameworks

>>:  The current better way to make select list all options when selected/focused

Recommend

Robots.txt detailed introduction

Robots.txt is a plain text file in which website ...

How to use union all in MySQL to get the union sort

Sometimes in a project, due to some irreversible ...

How to use Navicat to export and import mysql database

MySql is a data source we use frequently. It is v...

Continuous delivery using Jenkins and Docker under Docker

1. What is Continuous Delivery The software produ...

Linux echo text processing command usage and examples

The description of echo in the Linux help documen...

Sample code for implementing Alipay sandbox payment with Vue+SpringBoot

First, download a series of things from the Alipa...

mysql 5.7.20 win64 installation and configuration method

mysql-5.7.20-winx64.zipInstallation package witho...

Detailed explanation of Vue element plus multi-language switching

Table of contents Preface How to switch between m...

Summary of clipboard.js usage

Table of contents (1) Introduction: (2) The ways ...

Definition and usage of MySQL cursor

Creating a Cursor First, create a data table in M...

Two ways to clear table data in MySQL and their differences

There are two ways to delete data in MySQL: Trunc...

How to deploy Vue project using Docker image + nginx

1. Packaging Vue project Enter the following name...

Introduction to the use and disabling of transparent huge pages in Linux

introduction As computing needs continue to grow,...

Detailed analysis of the MySQL slow log opening method and storage format

In development projects, we can monitor SQL with ...