getInitialUser method

  1. @override
Future<AuthUserProfile?> getInitialUser()
override

Implementation

@override
Future<AuthUserProfile?> getInitialUser() async {
  var initUser = await FirebaseAuth.instance
      .authStateChanges()
      .first
      .timeout(1.seconds, onTimeout: () {
    log.info("Timed out after 1 second waiting for initial firebase auth");
    return null;
  });

  /// This takes a few seconds, but we do it way up here so we can sit on
  /// the landing screen longer.
  UserDetails? initProfile;
  if (initUser != null && !initUser.isAnonymous) {
    FirebaseApiAuth.user = initUser;
    try {
      initProfile = await loadUserProfile(initUser);
    } on ApiResponseException catch (e) {
      if (e.statusCode == kPreconditionRequired ||
          e.statusCode == kPreconditionFailed ||
          e.statusCode == kUnauthorized ||
          e.statusCode == kForbidden) {
        /// Leave this alone.  It just means that we need to
        /// finish account creation
        log.warning(
            "Found a firebase token, but couldn't authenticate against the server with it");
        await FirebaseAuth.instance.signOut();
        // Even though we appear to be authenticated, something failed in a request to the server, so let's force
        // a log out
        initUser = null;
        FirebaseApiAuth.user = null;
      } else {
        rethrow;
      }
    }
  }
  return AuthUserProfile(
    initUser,
    initProfile,
    AuthEventSource.initial,
    status: initUser != null ? AuthStatus.full : AuthStatus.none,
  );
}