我目前正在使用Nuxt.js及其随附的身份验证模块。
是否有可能在auth中创建豁免,以防止在用户未登录时自动重定向到登录页面?
以下面的示例为例:
用户尚未登录并导航至:http://localhost:3000/#/password-reset
它们将被重定向到登录屏幕:http://localhost:3000/#/login
我想创建一个例外,以便所有路由均已受auth保护,除了用于重置密码的页面(http://localhost:3000/#/password-reset)。
这怎么可能?
我假设您正在使用nuxt-auth。
创建中间件middleware/isAuth.js
:
export default function({ app, error }) {
if (!app.$auth.loggedIn) {
// If you want to redirect to login page
app.router.push('/password-reset')
// If you want to throw error, use this:
return error({
statusCode: 403,
message: 'You are not allowed to see this'
})
}
}
在您要保护的页面中,声明您的中间件:
export default {
...
middleware: ['isAuth'],
...
}
如果用户未登录,它将阻止页面。要登录用户,您应该使用loginWith
。