使用Firebase身份验证登录时如何保存用户数据?

时间:2020-05-05 18:38:55

标签: angular typescript firebase ionic-framework firebase-authentication

我正在使用Firebase身份验证来允许用户登录我的Angular应用。

下面是我的login()中的AuthService方法:

login(email: string, password: string) {
   return from(firebase.auth().signInWithEmailAndPassword(email, password));
}

这工作正常,但是现在我正尝试在用户登录时存储用户的数据,以便在刷新应用程序时不会意外注销用户。

有人可以告诉我如何使用上面方法返回的对象来确保用户保持登录状态,除非他们选择退出应用程序?

2 个答案:

答案 0 :(得分:1)

致电firebase.auth().signInWithEmailAndPassword(email, password)时,该用户将保持登录状态。您可以使用以下方法检查该用户是否为空:

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

https://firebase.google.com/docs/auth/web/manage-users

如果用户已登录,则只需在登录后将用户定向到页面即可。

答案 1 :(得分:0)

您必须保留数据。 看看这个文件:

https://firebase.google.com/docs/auth/web/auth-state-persistence

相关问题