我的应用程序有不同的路径:
GET /game/{any}此路由受Laravel身份验证中间件保护。在此Laravel路由中,我希望构建SPA并提供Vue路由器:
const routes = [
{ path: '/game/start', component: GameStart },
{ path: '/game/stats', component: GameStats }
]我有不受任何Laravel中间件保护的“main”路由
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/*‘路由器在前端部分不受保护。
或者我应该在前端使用Laravel Passport和token auth?
发布于 2018-12-11 06:38:44
您应该在前端使用Laravel Passport和令牌身份验证,并使用vue-router meta和callback (beforeEach)。
routes.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: '/' }
];router.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 routerhttps://stackoverflow.com/questions/52523478
复制相似问题