0

I'm developing a web app and I use Firebase Authentication for the authentication service.

The project seems to store the authentication, since if I refresh the page, or close the browser, the user is still logged in.

However I noticed that if I don't access the app for a long time (more than 1 hour, after the night for example), the authentication gets lost.

I don't know how to debug this and how to solve this.

Following some snippets of code to better understand my implementation:

This is the function I have in my startup view to redirect the user to the right page based on auth status.

bool isUserLoggedIn() {
    var user = _firebaseAuth.currentUser;
    return user != null;
  }

  void handleStartupBasedOnAuthStatus() {
    Future.delayed(const Duration(milliseconds: 1000), () async {
      bool loggedInShared =
          await sharedPreferences.getBoolSharedPreferences("loggedIn");
      if (isUserLoggedIn() || loggedInShared) {
        String ruoloValue =
            await sharedPreferences.getSharedPreferences('ruolo');
        (ruoloValue == Ruolo.ADMIN)
            ? navigationService.replaceWith(Routes.admin)
            : navigationService.replaceWith(Routes.messages);
      } else {
        navigationService.replaceWith(Routes.login);
      }
    });
  }

In the following function I call the onAuthStateChange to set sharedpreferences accordingly. I have the check on the timestamp because I noticed that it is triggered more time once the page is refreshed.

void listenToAuthChangesSharedPref() {
    FirebaseAuth.instance.authStateChanges().listen((firebaseUser) async {
      var datetimeNow = (DateTime.now().millisecondsSinceEpoch);
      String oldDatetimeString =
          await sharedPreferences.getSharedPreferences('previous_timestamp');
      if (oldDatetimeString != null) {
        var oldDatetime = (new DateTime.fromMillisecondsSinceEpoch(
                int.parse(oldDatetimeString)))
            .millisecondsSinceEpoch;

        if (datetimeNow - oldDatetime > 1000) {
          if (firebaseUser == null) {
            await sharedPreferences.setBoolSharedPreferences('loggedIn', false);
          } else {
            await sharedPreferences.setBoolSharedPreferences('loggedIn', true);
          }
          await sharedPreferences.setSharedPreferences(
              'previous_timestamp', datetimeNow.toString());
        }
      } else {
        if (firebaseUser == null) {
          await sharedPreferences.setBoolSharedPreferences('loggedIn', false);
        } else {
          await sharedPreferences.setBoolSharedPreferences('loggedIn', true);
        }
        await sharedPreferences.setSharedPreferences(
            'previous_timestamp', datetimeNow.toString());
      }
    });
  }

My question is: is possible that after long time currentUser and also the onAuthStateChanges gets called and the user is not logged in?

4

1 回答 1

0

保持身份验证状态# 适用于所有平台的 Firebase SDK 提供开箱即用的支持,以确保用户的身份验证状态在应用重新启动或页面重新加载时保持不变。

在 Android 和 iOS 等原生平台上,此行为是不可配置的,并且用户的身份验证状态将在应用重新启动之间保留在设备上。用户可以通过设备设置清除应用程序缓存的数据,这将擦除存储的任何现有状态。

在 Web 平台上,用户的身份验证状态存储在本地存储中。如果需要,您可以将此默认行为更改为仅保留当前会话的身份验证状态,或者根本不保留。要配置这些设置,请调用 setPersistence() 方法(注意;在本机平台上将抛出 UnimplementedError):

// 在 Web 平台上禁用持久性

await FirebaseAuth.instance.setPersistence(Persistence.NONE);

欲了解更多信息:

欲了解更多信息:

于 2021-04-10T07:30:11.363 回答