You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt结合Firestore登录后按用户类型跳转时始终重定向同一页面求助

问题原因
  • 错误1:firestore.collection('Users').where() 方法仅会构建Firestore查询对象,返回的不是布尔值,对象在if判断中永远为真,所以第一个判断条件永远触发,直接跳转/Form/SectionA
  • 错误2:调用users.get()后没有接收返回的查询快照数据,也没有提取当前登录用户的userType字段
  • 错误3:直接查询整个Users集合没有筛选当前登录用户,就算获取到数据也不能对应到当前登录的账号
修正方案

登录成功后先获取当前登录用户的UID,查询对应用户文档的userType字段后再做跳转判断,修正后的代码如下:

Login() {
  let credentials = {
    email: this.email,
    password: this.password,
  };
  this.$store
    .dispatch("user/Login", credentials)
    .then(async (user) => {
      // 取当前登录用户的UID,若vuex的login action没有返回user,也可以用firebase.auth().currentUser.uid获取
      const currentUid = user.uid || firebase.auth().currentUser.uid
      // 查询当前用户对应的文档
      const userDoc = await firestore.collection('Users').doc(currentUid).get()
      if (!userDoc.exists) {
        alert('用户信息不存在')
        return
      }
      const userData = userDoc.data()
      const userType = userData.userType
      // 按用户类型跳转,Nuxt推荐用$router.push避免整页刷新
      if (userType === "subcontractor") {
        this.$router.push("/Form/SectionA");
      } else if (userType === "contractor") {
        this.$router.push("/WPC/DashboardWPC");
      } else {
        this.$router.push("/BWG/DashboardBWG");
      }
  }).catch((err) => {
    alert(err.message)
  })
}
额外优化提示

如果你的vuex的user/Login action已经把用户信息存在store里了,可以直接从store取userType,不用重复请求Firestore,能提升登录响应速度。

内容的提问来源于stack exchange,提问作者user16136444

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 06:54:09