简体   繁体   English

Nuxt 看不到经过身份验证的用户

[英]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.

相关问题 如果未通过身份验证,则将用户重定向到页面加载时的登录屏幕(Nuxt-firebase) - Redirect users to login screen on page load if not authenticated (Nuxt-firebase) 用户未通过身份验证(放大) - The user is not authenticated (amplify) Amplify 中没有经过身份验证的用户的当前用户 - No current user for authenticated user in Amplify 如何从 Nuxt 前端访问经过身份验证的 GraphQL API - How to access authenticated GraphQL API from Nuxt frontend 将 Firebase 认证用户与 Flutter 中的“用户”集合链接 - Linking Firebase Authenticated user with 'users' collection in Flutter 在 Cloud Firestore 事务中,如果我们不知道文档名称,如何检查文档是否存在于特定集合中 - in Cloud Firestore transaction how Check to see if a Document exists in a specific Collection if we don't know Document name 我在 Firebase 控制台的实时数据库中看不到任何数据 - I don't see any data in my Realtime Database in Firebase's console 由于无效的字段值,我的 firebase 扩展无法安装,但我看不出它们有什么问题? - My firebase extension is failing to install due to invalid field values, but I don't see whats wrong with them? 发生存储异常。 用户未通过身份验证 - StorageException has occurred. User is not authenticated 错误:用户未在 nodejs 中对 amazon cognito 进行身份验证 - Error: User is not authenticated amazon cognito in nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM