简体   繁体   English

如何使用 Firebase 身份验证保持用户登录 Flutter web 应用程序

[英]How to keep a user logged in Flutter web app using Firebase Authentication

I am developing a FLutter web app and using Firebase Authentication with email and password login for signing in users我正在开发一个 FLutter web 应用程序并使用 Firebase 身份验证与 Z0C83F57C786A1B4A39EFAB27 登录用户登录

await FirebaseAuth.instance
      .signInWithEmailAndPassword(email: email, password: password);

After Successfully signing in, the code redirects the user to a HomePage.成功登录后,代码会将用户重定向到主页。

        Navigator.of(context).pushReplacementNamed('/HomePage');

But when the user refreshes the web page or tries to go to the website again, it has signed out and has to log in again.但是当用户刷新 web 页面或再次尝试 go 到该网站时,它已经退出并必须重新登录。

Following is the code I use to check the logged in user以下是我用来检查登录用户的代码

StreamBuilder<User?>(
    stream: FirebaseAuth.instance.userChanges(),
    initialData:  FirebaseAuth.instance.currentUser,
    builder: (context, snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        if (snapshot.data == null){
          Navigator.of(context).pushReplacementNamed('/HomePage');
        }else{
          Navigator.of(context).pushReplacementNamed('/signup');
        }
      }

How can I keep the user logged in as long as they use log out?只要用户使用注销,我怎样才能让用户保持登录状态? I want the solution to work specifically on flutter web.我希望该解决方案专门适用于 flutter web。

whatever you are building flutter app, web or desktop.无论您正在构建 flutter 应用程序、web 还是桌面。 you need to keep recorder of user in share preference.您需要将用户的记录器保留在共享偏好中。 this package shared_preferences: ^2.0.15 support web app too.这个 package shared_preferences: ^2.0.15 也支持 web 应用程序。 when user will logout then you must need to remove saved data.当用户注销时,您必须删除已保存的数据。 Click here:点击这里:

You should navigate to signup when data is null and to home otherwise but you are doing it reverse.当数据为 null 时,您应该导航到注册,否则导航到主页,但您正在反向操作。

builder: (context, snapshot) {
  if (snapshot.connectionState == ConnectionState.done) {
    if (snapshot.data == null){
      Navigator.of(context).pushReplacementNamed('/signup');
    }else{
      Navigator.of(context).pushReplacementNamed('/Homepage');
    }
  }

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

相关问题 如何在 Swift 中使用 Firebase 保持用户登录? - How to keep user logged in using Firebase in Swift? Firebase:如何让 Android 用户保持登录状态? - Firebase: How to keep an Android user logged in? 如何让用户登录 firebase 7.17.1 并做出反应? - How to keep user logged in with firebase 7.17.1 and react? Flutter Web Firebase 授权用户刷新后注销 - Flutter Web Firebase Auth user is logged out after refresh 如何在使用 firebase 永远登录到他的帐户后保持用户登录 - How to keep user logged in after logging in to his account forever using firebase Flutter App如何在Firebase中集成身份验证和数据库? - How to integrate authentication and database in Firebase for Flutter App? Flutter web 和 Firebase 身份验证类型错误:无法读取未定义的属性“应用程序” - Flutter web and Firebase authentication TypeError: Cannot read property 'app' of undefined Firebase 身份验证未在 Flutter Web 上持续 - Firebase Authentication is not persisted on Flutter Web 如何使用 Firebase 在 Flutter 中进行电话身份验证? - How to do Phone Authentication in Flutter using Firebase? 即使在具有 firebase 身份验证的 nextjs 应用程序中重新加载页面后,如何保持用户登录? - how can I keep a user looged in even after page reload in nextjs app with firebase authentication?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM