Vue3 Introduction Notes Seven - Login Function
This series of notes will focus on how to set up a project using Vue3 and interact with the backend API. It won’t cover the basic features of Vue. For the basic features of Vue, you can refer to this video Get a Quick Start with Vue in Four Hours. I got started with Vue by watching this video and thought it was pretty good.
Code address: https://github.com/yexia553/vue_study/tree/%E9%85%8D%E7%BD%AEvue-router/vue3-notes
The content of this note is a bit difficult. It is recommended to read it several times.
Be sure to read the code in the note in combination with the content of the GitHub repository and the videos mentioned in the blog. Otherwise, it may not be easy to understand.
This note uses several third - party packages. It is recommended that beginners execute cnpm install
in the project root directory to install these dependencies before reading the blog content to avoid errors later.
Introduction to the Login Function
Many websites have a login function. After visitors enter their account and password on the page, the page will request the backend API for authentication. If the authentication is successful, they will be redirected to the home page. Let’s briefly break down the specific steps in this process.
- First, there should be a login page where visitors can enter their account and password, and there should also be a login button.
- The backend should have an authentication - related API for the page to call to check whether the account and password provided by the visitor are correct.
- After the authentication check in the previous step passes, the front - end will obtain a token. This token indicates that the visitor is legitimate and needs to be stored for use when requesting the backend later.
The above is roughly the process of implementing a login function. In this process, axios will be used to call the API. In addition, JWT is used for backend authentication, and Vuex is needed for state management. I will introduce these three knowledge points separately below.
Axios Request to the API and Axios Encapsulation
People doing front - end development must have heard of axios. I won’t write about the introduction of axios as there is a lot of relevant content on the Internet.
The native axios requires writing a lot of code every time when calling the API. I have done some encapsulation, and the code is placed in src/api/request.js
,
1 |
|
Actually, this part of the encapsulation is not very good and has few additional functions, but the entire encapsulation idea is in it. It is highly recommended to understand the part of axios encapsulation in combination with the video How to Encapsulate Axios in Vue3. It is very useful in actual work. Before I came into contact with axios encapsulation, I wrote a lot of repetitive code every time I called an API. After modification, it is indeed much more convenient.
The tokenRefresher function in the above code is used to update the access token. You can ignore it for now and come back to understand this part after reading the following JWT and API parts.
Introduction to JWT and Its Application in the Login Function
Before reading the following content, it is recommended to carefully read the following two blogs in their entirety:
- [JSON Web Token Tutorial](https://www.ruanyifeng.com/blog/2018/07/json_web_token - tutorial.html) - Ruan Yifeng
- Introduction to JWT - Step by Step
You don’t need to delve into the details. Just understand the operation process.
I will briefly summarize the process of JWT in the login scenario to facilitate understanding the following content.
- After the visitor enters the account and password on the page and clicks login, the front - end will request the
/api/token/
API of the backend. If the authentication is successful, the backend will return an access token and a refresh token to the front - end. - The access token is used to access the backend API. Therefore, this token must be carried in subsequent requests to access the API normally. The refresh token is used to refresh the access token. Generally, the validity period of the access token is short. For example, in the Django DRF framework, by default, the validity period of the access token is only 5 minutes, while the validity period of the refresh token is 8 hours.
- After the front - end obtains the access token and refresh token, it needs to find a place to store them for later use, such as in cookies or localstorage.
- There should be a checking mechanism to check whether the access token has expired. If it has expired, the refresh token should be used to update it in a timely manner to obtain a new access token, and the access token stored in the previous step should also be updated.
Centralized Management of APIs in Vue3
In the case of front - end and back - end separation, the front - end generally needs a method to manage the called APIs, which is more convenient for later maintenance, updates, and modifications. Here, I will introduce a method I like. This method is suitable for small - to - medium - sized front - end projects where the number of called APIs is not very large.
- First, create a
config.js
file in thesrc/api
directory with the following content,
1 | /** |
It is not difficult to see that this file is used for environment management. For example, dev
represents the local development environment, test
represents the online testing environment, and prod
represents the production environment. You can also add other configurations according to your needs.
This config.js
is also used in the above - mentioned encapsulation of axios. You can go back and look at the code.
- Then, create an
api.js
file in thesrc/api
directory. This file is where all the called APIs will be placed. The code is as follows,
1 | import request from "./request.js"; |
The api.js
currently contains only two interfaces. One is login
, which is used when logging in, and the other is refreshtoken
, which is used to refresh the access token, as introduced in the JWT part above.
- After creating
api.js
, it also needs to be bound to Vue so that it can be called.
Modify thesrc/main.js
file. The modified content is as follows,
1 |
|
The modified parts are to introduce src/api/api.js
in the import
; and then configure api
to the global properties of Vue in the last line for easy reference later.
So far, the centralized management of APIs in Vue is completed.
Implementation of the Login Page
After so much preparatory work above, it is all for implementing the login page. Now let’s implement it. Create a login
folder in the src/views
directory, and then create a Login.vue
file in it. The content of the file is as follows. Pay attention to the comments in the code.
1 | <template> |
The above code implements the style and function of the page login, but we are still missing a route pointing to this page. Now let’s configure it. Modify the content of src/router/index.js
. The modified content is as follows,
1 |
|
In fact, only one place has been modified, that is, adding a first - level route `/login