Home  >  Article  >  Web Front-end  >  How to add token in vue

How to add token in vue

PHPz
PHPzOriginal
2023-04-13 10:26:202629browse

With the rapid development of front-end technology and the expansion of application scenarios, the Vue framework has become one of the important tools for web application development. During the application process, we often need to use tokens for authentication and security management. So, how does Vue add token? This article will introduce how to add tokens in Vue from the following aspects.

1. What is token
Token, also called token, is an access token used to ensure the security of users when operating between the client and the server. In Vue, token can be used to verify the user's identity and identify whether the user has access rights.

2. Use VueAxios to add token
VueAxios is a plug-in for Vue.js, used to communicate with the back-end API. We can use VueAxios to add tokens in Vue applications. The following is a code example for adding token:

import Vue from 'vue';
import axios from 'axios';
import VueAxios from 'vue-axios';

Vue.use(VueAxios, axios);

// 设置token
const token = 'your_token_here';
Vue.axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;

In the above example, we first introduce axios and VueAxios, and then use the Vue.use() method to register them into the Vue instance. Next we set the token and use Vue.axios.defaults.headers.common['Authorization'] = Bearer ${token} to set the request header. Bearer is one of the OAuth2.0 protocols. Authorization method, used to access protected resources after obtaining authorization.

3. Use the Vue-Auth plug-in to add token
Vue-Auth is a Vue.js plug-in that provides some methods that allow you to easily add authentication and authorization functions. The plugin supports multiple authentication modes, including JWT, OAuth2.0, etc. The following is a code example for adding token using the Vue-Auth plug-in:

import Vue from 'vue';
import VueAuth from '@websanova/vue-auth';
import axios from 'axios';

Vue.use(VueAuth, {
    auth: {
        request: function (req, token) {
            if(token){
                this.options.http._setHeaders.call(this, req, {Authorization: 'Bearer ' + token})
            }
        },
        response: function (res) {
            var token = res.data.token;
            if(token){
                return token;
            }
        }
    }
});

// 设置token
const token = 'your_token_here';
Vue.auth.token.set(token);

In the above example, we first introduce VueAuth and axios. Next, use the Vue.use() method to register VueAuth into the Vue instance and configure authentication options. The request option is used to set the request header, and the response option is used to set the server response. When the token is successfully obtained, it is saved.

Finally, we use the Vue.auth.token.set(token) method to save the token to the Vue instance for global use.

4. Use Cookies to Save Token
In addition to using VueAxios and Vue-Auth plug-ins, we can also use Cookies to save tokens. In Vue, we can use the vue-cookies plug-in to operate Cookies. The following is a code example of using Cookies to store tokens in Vue:

import Vue from 'vue';
import VueCookies from 'vue-cookies';

Vue.use(VueCookies);

// 设置token
const token = 'your_token_here';
Vue.$cookies.set('token', token);

In the above example, we first introduce the VueCookies plug-in. Then register it using the Vue.use() method. Finally, use the Vue.$cookies.set() method to save the token to Cookies. At this point, we can obtain the token stored in Cookies by accessing $cookies globally.

Summary:
The Vue framework provides a variety of ways to add tokens, and developers can choose according to actual needs. Whether using VueAxios, Vue-Auth plug-in, or using Cookies, we need to ensure the security of the token and update and verify it regularly. At the same time, how to add tokens to Vue applications is also one of the basic skills that front-end development engineers need to master.

The above is the detailed content of How to add token in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn