Backend: Laravel + Passport
Frontend:Vue 2.0 + Vuex + Vue-router
After logging in, the front-end obtains access_token
and saves it in localStorage
. What should the user do if they log out? Clear localStorage
? Do I need to send another request to the backend?
If the user does not click to log out, but directly closes the browser or window, the access_token
in localStorage
will still exist the next time he visits, which is not very secure. All right?
My access_token
is valid for one year, so it will be regenerated every time I log in. How to solve this problem?
Looking for ideas on how to handle front-end user authentication... Thank you very much! ! !
axios part
router part:
Delete the
localStorage
中的access_token
。可以给Vuex写个插件,每次
commit
mutation
时,更新一下access_token
refresh time when exiting.The next time you log in, determine the refresh time. If it is 5 minutes ago, the login information will be considered expired.
If you don’t want to put
access_token
inlocalStorage
, you can put it in Vuex, and you need to log in again every time.access_token
放到localStorage
中,可以放在Vuex中,每次都需要重新登录。重新登录时,你可以没必要都重新生成
access_token
When logging in again, you don’t need to regenerateaccess_token
.I just summarized a project, welcome to star~
[vue+axios] A project to learn how to implement login interception on the front end
The authentication information is subject to the background. Whether you are logging in or logging out, you must send a request, and then operate the front end based on the results returned by the api. If you do not remember the authentication information, it is better to use sessionStorage
Set a routing interceptor, intercept all pages except login and logout, check whether the local variable user exists, if it exists, determine the last verification time, and re-verify if it exceeds 1 minute.