restoredUid function

Future<String?> restoredUid({
  1. Duration timeout = const Duration(seconds: 3),
  2. Duration interval = const Duration(milliseconds: 50),
})

Waits briefly for a persisted session to be restored, returning its uid.

Auth::GetAuth returns before the secure store has been read, so current_user() is empty for a short window after init even when a session exists. Signing in during that window mints a new anonymous user and silently discards the persisted one — which looks exactly like persistence not working.

Polling rather than an auth-state listener: the restore either lands within a few hundred milliseconds or there is nothing stored, so a bounded wait answers the question without a callback to unregister. Returns null if no session was restored before timeout.

Implementation

Future<String?> restoredUid({
  Duration timeout = const Duration(seconds: 3),
  Duration interval = const Duration(milliseconds: 50),
}) async {
  final deadline = DateTime.now().add(timeout);
  while (DateTime.now().isBefore(deadline)) {
    final uid = currentUid();
    if (uid != null && uid.isNotEmpty) return uid;
    await Future<void>.delayed(interval);
  }
  return null;
}