Home > Web Front-end > JS Tutorial > body text

How to implement login registration and token verification using vue

亚连
Release: 2018-06-20 17:04:48
Original
5573 people have browsed it

In the Vue single page, we can monitor the route object, match the information from it to decide whether to verify the token, and then define subsequent behaviors. The following is an example code to share with you the Vue login registration and token verification functions. Friends who need it can refer to it

In most websites, login registration is achieved by combining local storage cookies, localStorage and request-time verification tokens, etc. technology. For some functional pages, it will try to obtain the token in the local storage for judgment. If it exists, you can enter, otherwise it will jump to the login page or pop up the login box.

In the Vue single page, we can monitor the route object, match the information from it to decide whether to verify the token, and then define subsequent behaviors.

The specific implementation code is as follows:

1. Use the router.beforeEach hook to determine whether the target route carries relevant meta information

// router.js
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const routes = [
 {
  path: '/',
  component: require('./views/Home'),
  meta: {
   requiresAuth: true
  }
 },
]
const router = new VueRouter({
 routes: routes
})
router.beforeEach((to, from, next) => {
 let token = window.localStorage.getItem('token') 
 if (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null)) {
  next({
   path: '/login',
   query: { redirect: to.fullPath }
  })
 } else {
  next()
 }
})
export default router
Copy after login

2. watch route object. The principle is the same as above.

<script>
  // App.vue
  export default {
    watch:{
      &#39;$route&#39;:function(to,from){
        let token = window.localStorage.getItem(&#39;token&#39;);
          if (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null)) {
            next({
             path: &#39;/login&#39;,
             query: { redirect: to.fullPath }
            })
          } else {
           next()
          }
      }
    }
  }
</script>
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to achieve preview effect in JS

##Use three.js to create a project

How to use ES6 syntax in Node (detailed tutorial)

Detailed introduction to the usage of this object in js

The above is the detailed content of How to implement login registration and token verification using vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!