[英]Nuxt don't see a authenticated user
I am doing user authentication, but I ran into a problem.我正在进行用户身份验证,但遇到了问题。 First, when loading, the middleware is loaded, it does not see the authorized user through $fire.auth.onAuthStateChanged.首先,加载时加载了中间件,它没有通过 $fire.auth.onAuthStateChanged 看到授权用户。 And if you go to another page (without reloading page), user is appear.如果你 go 到另一个页面(没有重新加载页面),用户就会出现。 How to make him see it at the first boot?如何让他在第一次启动时看到它?
Here is my middleware这是我的中间件
export default function ({app, route, redirect}) {
console.log('middleware')
app.$fire.auth.onAuthStateChanged(user => {
if (user) {
console.log('user+')
if (route.path === perm.signin || route.path === perm.signup) {
return redirect('/')
}
} else {
console.log('user-')
if (route.path !== perm.signin || route.path !== perm.signup) {
return redirect(perm.signin)
}
}
})
}
and what I received (1st pic) when first enter to app in console.log (middleware, user-).以及我第一次在console.log (中间件、用户)中进入应用程序时收到的信息(第一张图片)。 But if I go to another page I receive middleware, user+.但是,如果我将 go 转到另一个页面,我会收到中间件 user+。
I need user + to be on the first start我需要用户 + 才能第一时间开始
onAuthStateChanged fires the callback you provided as argument after the middleware has run. onAuthStateChanged 在中间件运行后触发您作为参数提供的回调。 In other words the callback's return statements are not being run when the middleware runs.换句话说,当中间件运行时,回调的返回语句不会运行。 You could either ensure the middleware is called after the first authentication attempt, but this would slow down the initial startup of the application.您可以确保在第一次身份验证尝试后调用中间件,但这会减慢应用程序的初始启动速度。 So you could expose the nuxt router to the onAuthStateChanged handler and router.push('/login') or router.push('/somewhere') from there.因此,您可以将 nuxt 路由器暴露给 onAuthStateChanged 处理程序和 router.push('/login') 或 router.push('/somewhere') 从那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.