token

Get to know the token

Not all interfaces can be unrestricted requests. When requesting certain interfaces that require permissions, credentials are required

What is a Token

A “token” (a long string generated by the back end) that is used to request interfaces that require permissions. It has the following two characteristics:

  • It is provided by the back-end interface

  • When a user logs in, it is returned by the back-end interface

The basic usage process of the Token

The user logs in to obtain the token

Save the token to the local PC

Initiate other requests, carrying tokens

2. Use tokens

Access to user profile this interface requires a token to access

1. Copy the token from the return value after successful login

2. Supplementary API

In API /user.js, add a getInfo function that calls the interface

// Get user profile
export const getProfile = () = > {
  return ajax({
    method: 'GET'.url: '/v1_0/user/profile'.headers: {
      // Authorization: 'Bearer token values' have Spaces between them
      Authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiO jE1OTM4NTIyMjgsInVzZXJfaWQiOjExMDI0OTA1MjI4Mjk3MTc1MDQsInJlZnJlc2giOm ZhbHNlfQ.gR880MifO8GIFG6PNh9eOZGGpfcwNRkK6MpI1upN93w'}})}Copy the code

3. Invoke the API function

In the SRC/views/login/index. Vue this component to call getInfo function, to take the user’s information

import {getProfile } from '@/api/user'

async doLogin () {
  // Try a call to get user profile
  getProfile()
}
Copy the code
  • Authorization and Bearer are both defined by the interface requirements of this program