简体   繁体   English

使用firebase认证登录时如何保存用户数据?

[英]How to save user data when they log in using firebase authentication?

I am using Firebase Authentication to allow users to log into my Angular app.我正在使用 Firebase 身份验证来允许用户登录我的 Angular 应用程序。

Below is the login() method in my AuthService :下面是我的AuthService中的login()方法:

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

This is working fine, but now I am trying to store the user's data when they log in, so that they're not accidentally logged out when they refresh the app.这工作正常,但现在我试图在用户登录时存储他们的数据,以便他们在刷新应用程序时不会意外注销。

Can someone please tell me how I can use the object returned in the above method to ensure a user remains logged in unless they choose to log out of the app?有人可以告诉我如何使用上述方法返回的 object 来确保用户保持登录状态,除非他们选择退出应用程序?

When you call firebase.auth().signInWithEmailAndPassword(email, password) then the user will stay logged in. You can check if the user is null or not by using:当您调用firebase.auth().signInWithEmailAndPassword(email, password)时,用户将保持登录状态。您可以使用以下命令检查用户是否为 null:

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 https://firebase.google.com/docs/auth/web/manage-users

If user is signed in,then just direct the user to the pages after login.如果用户已登录,则只需在登录后将用户定向到页面。

You have to persist the data.您必须保留数据。 Look this doc:看这个文档:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Ionic / Angular 应用程序中使用 firebase 身份验证时如何存储其他用户信息? - How to store additional user info when using firebase authentication in Ionic / Angular app? 使用 firebase 和 angular 进行身份验证。 用户登录后如何导航到其他组件? - Authentication with firebase and angular. How to navigate to other component after the user log in? 更改用户 Firebase 身份验证时触发的事件 - Event triggered when changing a user Firebase Authentication 使用Firebase身份验证终止用户会话 - Expire user session using Firebase Authentication 如何在用户使用Angular PWA离线时处理身份验证? - How to handle authentication when user is offline using Angular PWA? 如何使用Angular 4构建数据以从Firebase检索用户收藏夹 - How to Structure Data for Retrieving User Favorites from Firebase using Angular 4 用户注销时的警报消息 Firebase 身份验证在 angular 14 - Alert Message When user logged out Firebase authentication in angular 14 使用 Google 身份验证时出现 Angular Firebase 用户错误 - Angular firebase user error while using google authentication 在 Firebase 中创建用户并同时在 Angular 中将其数据保存在 Firestore 中 - Create user in Firebase and save its data in Firestore at the same time in Angular 如何使用Firebase和Ionic和Cordova记录事件 - How to log events using Firebase with Ionic and Cordova
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM