01aa362d3c
Previously, if the backend was unreachable at launch, the splash screen routed to the login screen. The server URL remained persisted in SharedPreferences but visually appeared lost, frustrating any user who isn't the service operator. - New AuthStatus.offline distinguishes network failures (NetworkException) from HTTP 401 in AuthNotifier.verify(). - Persist has_ever_logged_in flag on first successful verify/login. - Offline + ever-logged-in lands on the briefing with a sticky offline banner (retry button) instead of being punted to login. - OfflineBanner widget is Tier-2-ready so we can surface "last sync X min ago" once real caching lands (Fable task #147).
46 lines
1.3 KiB
Dart
46 lines
1.3 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../core/exceptions.dart';
|
|
import 'api_client_provider.dart';
|
|
import 'settings_provider.dart';
|
|
|
|
enum AuthStatus { unknown, authenticated, unauthenticated, offline }
|
|
|
|
final authProvider = NotifierProvider<AuthNotifier, AuthStatus>(AuthNotifier.new);
|
|
|
|
class AuthNotifier extends Notifier<AuthStatus> {
|
|
@override
|
|
AuthStatus build() => AuthStatus.unknown;
|
|
|
|
Future<void> verify() async {
|
|
try {
|
|
final repo = ref.read(authRepositoryProvider);
|
|
final ok = await repo.verify();
|
|
if (ok) {
|
|
await ref.read(hasEverLoggedInProvider.notifier).markLoggedIn();
|
|
}
|
|
state = ok ? AuthStatus.authenticated : AuthStatus.unauthenticated;
|
|
} on NetworkException {
|
|
state = AuthStatus.offline;
|
|
} catch (_) {
|
|
state = AuthStatus.unauthenticated;
|
|
}
|
|
}
|
|
|
|
Future<void> login(String username, String password) async {
|
|
final repo = ref.read(authRepositoryProvider);
|
|
await repo.login(username, password);
|
|
await ref.read(hasEverLoggedInProvider.notifier).markLoggedIn();
|
|
state = AuthStatus.authenticated;
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
try {
|
|
final repo = ref.read(authRepositoryProvider);
|
|
await repo.logout();
|
|
} finally {
|
|
state = AuthStatus.unauthenticated;
|
|
}
|
|
}
|
|
}
|