Vue Authentication Login

Source: Internet
Author: User
Tags sessionstorage

Vue Login Routing Verification

The login status of Vue's project if it is managed with Vuex state, the state of the page refresh Vuex management disappears, so the login route verification is meaningless. You can write the status of the login to the Web storage for storage management.

The steps are as follows:

1, in the login component, the login status is written to the Web storage. (Generally written session Storage, sessions closed, stored data automatically deleted)

if (' Login successful ')

{Sessionstorage.setitem (' Accesstoken '), writing a login token that was successfully returned by the login or the custom judgment string}

2. Join the login Verification identifier Requiresauth (custom) in the routing meta information that requires login authentication

[HTml] routers: [                    { path:  '/listinfo ',                      name:  ' Listinfo ',                      component:  listInfo,                      meta: {  requiresAuth: true                                    }                       }                  ]     View plain  copy

3. Verify that the page needs to be logged in the Global hook function Beforeeach

Router.beforeeach (to, from, next) = = {

To the target route object to be entered, the route from which the current navigation is going to leave, Next: function hooks for next execution

if (To.path = = = '/login ') {Next ()}//If the login route is about to be entered, direct release

else {//entered is not a login route

if (To.meta.requiresAuth &&!sessionstorage.getitem (' Accesstoken ')) {next {path: '/login '}}

The next-hop route requires logon authentication and is not yet logged in, the route is directed to the login route

else {next ()}}//If login verification is not required, or if login is successful, direct release

}

Note: Beforeeach This global hook is placed in front of the global component and placed behind the global component, and the Vue instance is already loaded. When you enter the route to go directly in the address bar of the browser, you will not be directed to the login route.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.