import 'package:moneymgr_mobile/routes/login/login_model.dart'; import 'package:moneymgr_mobile/services/router/routes_list.dart'; import 'package:moneymgr_mobile/services/storage/secure_storage.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; part 'auth_state.g.dart'; /// The current authentication state of the app. /// /// This notifier is responsible for saving/removing the token and profile info /// to the storage through the [login] and [logout] methods. @riverpod class CurrentAuthState extends _$CurrentAuthState { @override AuthState build() { final secureStorage = ref.watch(secureStorageProvider).requireValue; final token = secureStorage.get('token'); return token != null ? AuthState.authenticated : AuthState.unauthenticated; } /// Attempts to log in with [data] and saves the token and profile info to storage. /// Will invalidate the state if success. Future login(LoginModel data) async { // TODO : perform login /*final secureStorage = ref.read(secureStorageProvider).requireValue; final token = await ref.read(apiServiceProvider).login(data); // Save the new [token] and [profile] to secure storage. secureStorage.set('token', token); ref // Invalidate the state so the auth state will be updated to authenticated. ..invalidateSelf() // Invalidate the token provider so the API service will use the new token. ..invalidate(tokenProvider);*/ } /// Logs out, deletes the saved token and profile info from storage, and invalidates /// the state. void logout() { // TODO : implement logic /*final secureStorage = ref.read(secureStorageProvider).requireValue; // Delete the current [token] and [profile] from secure storage. secureStorage.remove('token'); ref // Invalidate the state so the auth state will be updated to unauthenticated. ..invalidateSelf() // Invalidate the token provider so the API service will no longer use the // previous token. ..invalidate(tokenProvider);*/ } } /// The possible authentication states of the app. enum AuthState { unknown(redirectPath: homePage, allowedPaths: [homePage]), unauthenticated( redirectPath: authPage, allowedPaths: [authPage, settingsPage], ), authenticated(redirectPath: homePage, allowedPaths: null); const AuthState({required this.redirectPath, required this.allowedPaths}); /// The target path to redirect when the current route is not allowed in this /// auth state. final String redirectPath; /// List of paths allowed when the app is in this auth state. May be set to null if there is no /// restriction applicable final List? allowedPaths; }