restoreSession method

Future<void> restoreSession()

Restore session — called on app launch (auto-called during init)

Implementation

Future<void> restoreSession() async {
  if (tokenManager.hasTokens) {
    authState.setAuthenticated();

    // When mocking, skip real profile fetch (endpoints may be blank)
    if (config.mockAuth) return;

    await config.onTokenRestored?.call();

    // Restore cached profile immediately (no blank screen)
    await _profileExtractor.restoreProfile();

    // Run custom/optional API auth validation before completing routing
    var valid = true;
    if (config.customAuthValidator != null) {
      valid = await config.customAuthValidator!();
    }

    if (!valid) {
      await logout();
      return;
    }

    // Then fetch fresh profile in background
    _profileExtractor.fetchProfile(
      config.endpoints.getProfile,
      config.endpoints.getProfileMethod,
    );
  } else {
    authState.setUnauthenticated();
  }
}