38 lines
1.1 KiB
Dart
38 lines
1.1 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../../app/error_message.dart';
|
|
import '../../../app/load_state.dart';
|
|
import '../../../models/auth_info.dart';
|
|
import '../../shared/providers/core_providers.dart';
|
|
|
|
final authInfoProvider =
|
|
NotifierProvider<AuthInfoNotifier, LoadState<AuthInfo?>>(
|
|
() => AuthInfoNotifier(),
|
|
);
|
|
|
|
class AuthInfoNotifier extends Notifier<LoadState<AuthInfo?>> {
|
|
@override
|
|
LoadState<AuthInfo?> build() => const LoadState.idle(null);
|
|
|
|
Future<AuthInfo?> load({bool failOnError = false}) async {
|
|
state = LoadState.loading(state.data);
|
|
try {
|
|
final api = ref.read(apiProvider);
|
|
final res = await api.getAuthMe();
|
|
final authInfo = AuthInfo.fromApi(res.data);
|
|
state = LoadState.data(authInfo);
|
|
return authInfo;
|
|
} catch (e) {
|
|
state = LoadState.error(null, describeLoadError(e));
|
|
if (failOnError) rethrow;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
void clear() => state = const LoadState.idle(null);
|
|
|
|
void restore(LoadState<AuthInfo?> restoredState) => state = restoredState;
|
|
|
|
bool get isAdmin => state.data?.isAdmin == true;
|
|
}
|