Muezz
06/04/2022, 3:17 AMStateNotifier
from riverpod and sets state according to each method called:
dart
class AuthController extends StateNotifier<AuthState> {
final AuthService _authService;
AuthController(AuthService authService)
: _authService = authService,
super(const AuthState.loading()) {
checkUserAuth();
}
void checkUserAuth() {
if (_authService.currentUser == null) {
state = const AuthState.unauthenticated();
} else {
state = const AuthState.authenticated();
}
}
Future<void> loginUser(String username, String password) async {
state = const AuthState.loading();
Result result =
await _authService.loginWithEmail(email: username, password: password);
if (!result.isError) {
state = const AuthState.authenticated();
} else {
state = const AuthState.unauthenticated();
}
}
Future<Result> logoutUser() async {
await _authService.logOut().onError(
(error, stackTrace) => Result.error(message: 'ERROR: $error'),
);
state = const AuthState.unauthenticated();
return Result.success(message: 'SUCCESS: You logged out!');
}
}