我正在使用laravel身份验证来确定用户看到的内容。
我想要什么:
master.blade.php
@Guest
<navigationloggedout></navigationloggedout>
<p>Not logged in</p>
<router-view></router-view>
@else
<navigationloggedin></navigationloggedin>
<p>Logged in</p>
<router-view></router-view>
@endGuest
如何仅允许用户在未登录时访问某些vue页面。如何为未登录的用户设置不同的默认路径。 下面的路由显示了用户登录后的默认组件。
路线:
{
path: '/',
component: home_logged_in
}
答案 0 :(得分:1)
我使用路由器保护(https://router.vuejs.org/guide/advanced/navigation-guards.html)将路由器用于身份验证
const router = new Router({
mode: "history",
routes: [
{
path: "/open-page",
name: "openPage",
component: OpenPage
},
{
path: "/secure-page",
name: "securePage",
component: SecurePage,
meta: {
authRequired: true
}
}
]
});
router.beforeEach((to, from, next) => {
if (!to.meta.authRequired) {
next();
} else if (store.getters["user/isAuthenticated"]) {
next();
} else {
next({
path: "/open-page"
});
}
});