resolveInitialStatus method
Resolves the initial AuthenticationStatus and user at startup.
Checks SecureStorage for a stored token and handles three cases:
- No token → unauthenticated.
- Refresh token expired → clears storage, unauthenticated.
- Valid token → fetches user from API; falls back to cached user if the API is unreachable (except for 401/403, which force unauthenticated).
Returns a tuple of the resolved AuthenticationStatus and the
UserModel if available, or null.
Called once by AuthCheckTask during the startup sequence.
Implementation
Future<(AuthenticationStatus, UserModel?)> resolveInitialStatus() async {
_logger.info('[AuthRepository] Resolving initial auth status');
try {
final AuthTokenModel? token = await _secureStorage.getAuthTokenModel();
if (token == null || token == AuthTokenModel.empty) {
_logger.info('[AuthRepository] No token found — unauthenticated');
Sentry.metrics.count('auth.startup.no_token', 1);
return (_emitStatus(AuthenticationStatus.unauthenticated), null);
}
if (token.isRefreshTokenExpired) {
_logger.warn('[AuthRepository] Refresh token expired — clearing data');
await _clearAuthData();
Sentry.metrics.count('auth.startup.token_expired', 1);
return (_emitStatus(AuthenticationStatus.unauthenticated), null);
}
_logger.info('[AuthRepository] Valid token found — resolving user');
return await _resolveUserForStartup();
} catch (e, st) {
_logger.error('[AuthRepository] Unexpected error during initial status resolution', error: e, stackTrace: st);
return (_emitStatus(AuthenticationStatus.unauthenticated), null);
}
}