gpt4 book ai didi

javascript - NuxtJS 和 Auth 路由豁免

转载 作者:行者123 更新时间:2023-12-02 22:32:56 28 4
gpt4 key购买 nike

我目前正在使用 Nuxt.js 及其附带的身份验证模块。

是否可以在身份验证中创建豁免,以防止用户未登录时自动重定向到登录页面?

以下为例:

  1. 用户未登录并导航至:http://localhost:3000/#/password-reset

  2. 他们将被重定向到登录屏幕:http://localhost:3000/#/login

我想创建一个豁免,以便所有路由都受到身份验证的保护,它们已经受到保护,但重置密码的页面 ( 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 .

关于javascript - NuxtJS 和 Auth 路由豁免,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58836600/

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