gpt4 book ai didi

javascript - Vue 路由器和 Laravel 中间件

转载 作者:搜寻专家 更新时间:2023-10-30 22:28:13 25 4
gpt4 key购买 nike

我的申请有不同的路线:

GET /game/{any}

此路由受 Laravel 身份验证中间件保护。在这个 Laravel 路由中,我想构建 SPA 并提供 Vue 路由器:

const routes = [
{ path: '/game/start', component: GameStart },
{ path: '/game/stats', component: GameStats }
]

我有一个不受任何 Laravel 中间件保护的“主”路由

GET /{any}

整个 Vue 路由器看起来像这样:

const routes = [
// Not protected URLs
{ path: '/', component: Main },
{ path: '/news', component: News },

// Protected URLs
{ path: '/game/start', component: GameStart },
{ path: '/game/stats', component: GameStats }
]

所以我的问题是:像这样混合后端和前端是个好主意吗?因为我假设“/game/*”路由器在前端部分不 protected 。

或者我应该在前端使用 Laravel Passport 和 token auth 吗?

最佳答案

您应该使用 vue-router meta 和回调(beforeEach)在前端使用 Laravel Passport 和 token 身份验证。

路由.js

...

export const routes = [
{ path: '/game/start', component: GameStart, meta: { requiresAuth: true } },
{ path: '/game/stats', component: GameStats, meta: { requiresAuth: true } },
{ path: '/signin', component: SigninPage },
{ path: '*', redirec: '/' }
];

路由器.js

import VueRouter from 'vue-router';
import { routes } from './routes';
import store from './store'

export const router = new VueRouter({
routes,
mode: 'history'
});

router.beforeEach((to, from, next) => {
// you could define your own authentication logic with token
let isAuthenticated = store.getters.isAuthenticated

// check route meta if it requires auth or not
if(to.matched.some(record => record.meta.requiresAuth)) {
if (!isAuthenticated) {
next({
path: '/signin',
params: { nextUrl: to.fullPath }
})
} else {
next()
}
} else {
next()
}
})

export default router

关于javascript - Vue 路由器和 Laravel 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52523478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com