【问题标题】:Nuxtjs and Firebase Auth: await firebase.auth().currentUser not waiting?Nuxtjs 和 Firebase 身份验证:等待 firebase.auth().currentUser 不等待?
【发布时间】:2019-03-14 09:55:15
【问题描述】:

Nuxt.js 专注于服务器端渲染,有一个asyncData 属性,在页面组件加载之前调用一次。

我正在尝试类似的东西:

async asyncData({params}) {
    // firebase.auth().onAuthStateChanged((user)=>{ // <-- this doesn't work in the asyncData property
      let user = await firebase.auth().currentUser
      let info = {}
      console.log(user)

      user.uid === null // true
}

两个类似的问题:

有一些似乎不适用于 nuxt 的解决方案...

我也试过了:

function getCurrentUser(auth) {

  let userLoaded = false;
  return new Promise((resolve, reject) => {
     if (userLoaded) {
          resolve(firebase.auth().currentUser);
     }
     const unsubscribe = auth.onAuthStateChanged(user => {
        userLoaded = true;
        unsubscribe();
        resolve(user);
     }, reject);
  });
}

【问题讨论】:

  • 我认为问题出在onAuthStateChanged 没有返回承诺但添加观察者 以更改用户的登录状态,请参阅firebase.google.com/docs/reference/js/…
  • @RenaudTarnec 是的,这就是为什么我也尝试不使用它的原因...我也将 currentUser 包装在一个承诺中,但它不起作用
  • 我正在尝试实现同样的目标。有更新吗?
  • 我相信它不会那样工作。 asyncData() 第一次加载时在服务器端运行。服务器端不会有任何 currentUser 实例。它仅在客户端可用。

标签: firebase vuejs2 firebase-authentication nuxt.js


【解决方案1】:

似乎onAuthStateChanged 仅在客户端触发。但问题是,SSR 功能仅对未经过身份验证的用户有意义,对于经过身份验证的用户场景,不妨将 firebase 调用逻辑放入 mounted 挂钩中。

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    相关资源
    最近更新 更多