a687e3637f
Migrate StateNotifierProvider/StateNotifier → NotifierProvider/Notifier, StateProvider → NotifierProvider with simple notifier, FamilyAsyncNotifier removed in Riverpod 3 (replaced with constructor-arg pattern), and rename .valueOrNull → .value throughout all providers and screens. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
38 lines
1015 B
Dart
38 lines
1015 B
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'api_client_provider.dart';
|
|
|
|
enum AuthStatus { unknown, authenticated, unauthenticated }
|
|
|
|
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();
|
|
state = ok ? AuthStatus.authenticated : AuthStatus.unauthenticated;
|
|
} catch (_) {
|
|
state = AuthStatus.unauthenticated;
|
|
}
|
|
}
|
|
|
|
Future<void> login(String username, String password) async {
|
|
final repo = ref.read(authRepositoryProvider);
|
|
await repo.login(username, password);
|
|
state = AuthStatus.authenticated;
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
try {
|
|
final repo = ref.read(authRepositoryProvider);
|
|
await repo.logout();
|
|
} finally {
|
|
state = AuthStatus.unauthenticated;
|
|
}
|
|
}
|
|
}
|