使用laravel auth保护Vue路由

时间:2020-04-16 11:49:45

标签: laravel vue.js

我正在尝试保护某些vue组件,以便用户必须先登录才能查看它。问题是,即使 window.laravel.islogin false 并且他们尚未登录,用户仍然可以看到该组件。

App.js

const routes = [
{path:'/',component: require('./components/Home.vue').default},
{path:'/form',component: require('./components/Form.vue').default, meta: {
    authRequired: true
}},
]
const router = new VueRouter({
   routes,
   mode: 'history'
});

router.beforeEach((to, from, next) => {
if (to.meta.authRequired) {
    if(window.laravel.isLogin){
        console.log(window.laravel.isLogin) //returns true or false
        next()
    }else{
        next('/login')
    }
}else {
    next()
}
});

App.blade.php

<script>
window.laravel = @json(['isLogin' => auth()->check() ? 'true' : 'false']);
</script>

0 个答案:

没有答案