Nuxtjs和Firebase身份验证:正在等待firebase.auth()。currentUser不等待吗?

时间:2019-03-14 09:55:16

标签: firebase vuejs2 firebase-authentication nuxt.js

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);
  });
}

0 个答案:

没有答案