$ firebaseAuth()。$ getAuth()返回null

时间:2016-11-23 12:03:30

标签: angularjs firebase firebase-authentication angularfire

var firebaseUser = $firebaseAuth().$getAuth();
if (firebaseUser) {
console.log("Signed in as:", firebaseUser.uid);
} else {
  console.log("Signed out");}
在我刷新页面2次后,

返回null,即使用户仍然登录。如果我获得Firebase用户的UID在我的实时数据库中添加一些数据,因为它&# 39; s null,我的应用会有错误

1 个答案:

答案 0 :(得分:3)

要获得当前用户,您应该这样做:

var user = firebase.auth().currentUser;

if (user) {
  // User is signed in.
} else {
  // No user is signed in.
}

或者您可以处理身份验证:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

所有这些信息均来自here