This commit is contained in:
Phuoc Nguyen
2025-10-10 17:40:53 +07:00
parent bdaf0b96c5
commit 10ccd0300d
3 changed files with 337 additions and 8 deletions

View File

@@ -60,18 +60,20 @@ class AuthState {
bool? isAuthenticated,
bool? isLoading,
String? errorMessage,
bool clearUser = false,
bool clearError = false,
}) {
return AuthState(
user: user ?? this.user,
user: clearUser ? null : (user ?? this.user),
isAuthenticated: isAuthenticated ?? this.isAuthenticated,
isLoading: isLoading ?? this.isLoading,
errorMessage: errorMessage ?? this.errorMessage,
errorMessage: clearError ? null : (errorMessage ?? this.errorMessage),
);
}
}
/// Auth state notifier provider
@riverpod
@Riverpod(keepAlive: true)
class Auth extends _$Auth {
@override
AuthState build() {
@@ -119,13 +121,14 @@ class Auth extends _$Auth {
required String email,
required String password,
}) async {
state = state.copyWith(isLoading: true, errorMessage: null);
state = state.copyWith(isLoading: true, clearError: true);
final result = await _repository.login(email: email, password: password);
return result.fold(
(failure) {
state = state.copyWith(
isAuthenticated: false,
isLoading: false,
errorMessage: failure.message,
);
@@ -150,7 +153,7 @@ class Auth extends _$Auth {
required String password,
List<String> roles = const ['user'],
}) async {
state = state.copyWith(isLoading: true, errorMessage: null);
state = state.copyWith(isLoading: true, clearError: true);
final result = await _repository.register(
name: name,
@@ -162,6 +165,7 @@ class Auth extends _$Auth {
return result.fold(
(failure) {
state = state.copyWith(
isAuthenticated: false,
isLoading: false,
errorMessage: failure.message,
);
@@ -181,7 +185,7 @@ class Auth extends _$Auth {
/// Get user profile (refresh user data)
Future<void> getProfile() async {
state = state.copyWith(isLoading: true, errorMessage: null);
state = state.copyWith(isLoading: true, clearError: true);
final result = await _repository.getProfile();
@@ -195,6 +199,7 @@ class Auth extends _$Auth {
(user) {
state = state.copyWith(
user: user,
isAuthenticated: true,
isLoading: false,
);
},