Two minor problems of page login

1: after the page login is redirected, the user switches routes, how to ensure that he does not have to log in every time?

2: the user enters a routing link at will for the first time, how to judge that he has not logged in yet, and needs to log in.


the pure switching page does not need to determine whether the user is logged in or not, nor does it make sense. It is just that the page with user information in the page needs to determine whether to log in or not (that is, the page that needs to be removed from the interface). After a successful login, the backend usually puts a login status value in the cookie. When the ajax requests the backend, the API will first obtain the cookie value and check whether the user is logged in to the database. The API will return the unlogged status. The front end needs to define the logic of the returned status of the unlogged in in the post and get of the public ajax.


save the token generated by login in cookie , and take other requests to determine whether to log in or not.
or save the token locally and bring it with you when requested.


determines the route, restricts whether you need to log in to the current page, stores it locally, and writes it in the token of the request header.


your problem is how to design a reasonable login method for users. This problem is generally solved by session and cookie (what exactly is the latter? search): when each user visits your site using the same browser, he / she will bring his / her cookie on your site, and this cookie contains the session id that you assigned to the user (if not, It means that this user is visiting your website for the first time, so you should assign him a new session id ). When the user logs in, the server records the user's login status in the corresponding session , such as the following tuple: (user ID, login time, login status expiration time)-- this login status information is recorded in the database on the server. Later, when the user visits your website again, because the cookie contains session id , the server reads and restores the user's login status to the database through this id .

if you don't understand, please first figure out what cookie is, then ask questions, and I'll give you another example.


first of all, Token, puts the sessionId in the response message cookie, as mentioned above. These are set by the backend. Each request message you request will automatically bring the cookie, and the backend will verify it. Generally return 0 and 1. All this is done by the backend.
what the front end needs to do is to verify when the user logs in to some web pages that require permission. At this time, you can use routing, such as route guard in vue.js

.
Menu