使用Nuxt-Auth,是否有官方的方法来检查路由是否受保护(即需要登录)?
在一些示例中间件中,执行"route“的控制台日志会显示一个很大的JSON有效负载,其中提到了middleware: 'auth'。然而,这种方法的可靠性如何呢?如果将来JSON结构发生变化怎么办?
export default function({ $auth, route }) {
// This returns 'auth' for protected routes; undefined for unprotected routes.
// But how reliable is this method? The JSON structure could change in the future.
console.log(route.matched[0].components.default.options.middleware)
}发布于 2021-11-15 03:54:18
我找到了一个适合我的解决方案。也许这会对你有帮助。auth-next/runtime.js文件中有一个函数。我已经把它复制到我的文件里了
function routeOption(route, key, value) {
return route.matched.some((m) => {
if (process.client) {
return Object.values(m.components).some((component) => component.options && component.options[key] === value)
} else {
return Object.values(m.components).some((component) => Object.values(component._Ctor).some((ctor) => ctor.options && ctor.options[key] === value))
}
})
}
然后你可以这样使用它:
console.log('Auth not required: ', routeOption(ctx.route, 'auth', false))
https://stackoverflow.com/questions/69059693
复制相似问题