Thursday, March 16, 2017

router.beforeEach in laravel 5.4

(using laravel 5.4 and vue.js 2x)

I use router.beforeEach() to handle authorization in my app. But, my function router.beforeEach() is only loading on the moment after login. If I refresh the page the function isn't called again.

Here is my code:

import router from './routes.js';
require('./bootstrap');

const app = new Vue({
    el: '#app',
    router,
});


router.beforeEach((to,from,next) => {
    if(to.meta.requiresAuth){
        authUser = '';
        const authUser = JSON.parse(window.localStorage.getItem('authUser'))
        if(authUser && authUser.access_token){
            next()
        }else{
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            })
        }
    }
    next()
})

The full project: https://github.com/jrpikong/wingding



via user3230375

Advertisement