@@ -9,65 +9,21 @@ import '../../shared/repository/hive_repository.dart';
99part 'auth_event.dart' ;
1010part 'auth_state.dart' ;
1111
12- // class AuthBloc extends Bloc<AuthEvent, AuthState> {
13- // final AuthRepository authRepository;
14- // AuthBloc(this.authRepository) : super(AuthInitial()) {
15- // on<AuthEvent>((event, emit) async {
16- // if (event is LoginUser) {
17- // try {
18- // emit(AuthLoading());
19- // await authRepository.loginWithEmailAndPassword(
20- // event.username, event.password);
21- //
22- // emit(AuthLoaded(AuthPurpose.LOGIN));
23- // } catch (e) {
24- // debugPrint(e.toString());
25- // emit(
26- // AuthLoadingError(
27- // e.toString(),
28- // ),
29- // );
30- // }
31- // } else if (event is RegisterUser) {
32- // try {
33- // emit(AuthLoading());
34- //
35- // await authRepository.registerWithEmailAndPassword(event.model);
36- //
37- // emit(AuthLoaded(AuthPurpose.REGISTER));
38- // } catch (e) {
39- // debugPrint(e.toString());
40- // emit(
41- // AuthLoadingError(
42- // e.toString(),
43- // ),
44- // );
45- // }
46- // } else if (event is UseAsGuest) {
47- // emit(GuestUser());
48- // }
49- // });
50- // }
51- // }
5212class AuthBloc extends Bloc <AuthEvent , AuthState > {
5313 final AuthRepository authRepository;
5414
5515 AuthBloc (this .authRepository) : super (AuthInitial ()) {
56- //debugPrint("AuthBloc initialized");
57-
5816 on < AppStarted > (_onAppStarted);
5917
60-
6118 on < LoginUser > (_onLoginUser);
6219
63-
6420 on < RegisterUser > (_onRegisterUser);
6521
22+ on < LogoutUser > (_onLogoutUser);
6623
6724 on < UseAsGuest > ((event, emit) => emit (GuestUser ()));
6825 }
6926
70-
7127 Future <void > _onAppStarted (AppStarted event, Emitter <AuthState > emit) async {
7228 emit (AuthLoading ());
7329 try {
@@ -85,15 +41,15 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
8541 }
8642 }
8743
88-
8944 Future <void > _onLoginUser (LoginUser event, Emitter <AuthState > emit) async {
9045 emit (AuthLoading ());
9146 try {
92-
93- final token = await authRepository. loginWithEmailAndPassword ( event.username, event.password);
47+ final token = await authRepository. loginWithEmailAndPassword (
48+ event.username, event.password);
9449 await HiveRepository .saveData (HiveBoxNames .authBox, 'token' , token);
9550 // Save token in Hive
96- final savedToken = await HiveRepository .getData ('token' , HiveBoxNames .authBox);
51+ final savedToken =
52+ await HiveRepository .getData ('token' , HiveBoxNames .authBox);
9753 //debugPrint("Saved token: $savedToken");
9854
9955 emit (AuthLoaded (AuthPurpose .LOGIN ));
@@ -103,8 +59,8 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
10359 }
10460 }
10561
106-
107- Future < void > _onRegisterUser ( RegisterUser event, Emitter <AuthState > emit) async {
62+ Future < void > _onRegisterUser (
63+ RegisterUser event, Emitter <AuthState > emit) async {
10864 emit (AuthLoading ());
10965 try {
11066 await authRepository.registerWithEmailAndPassword (event.model);
@@ -116,6 +72,18 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
11672 }
11773
11874
75+ Future <void > _onLogoutUser (LogoutUser event, Emitter <AuthState > emit) async {
76+ emit (AuthLoading ());
77+ try {
78+ await HiveRepository .deleteData (
79+ 'token' , HiveBoxNames .authBox); // Remove token from Hive
80+ emit (GuestUser ()); // Emit guest state after logout
81+ } catch (e) {
82+ debugPrint ("Logout error: $e " );
83+ emit (AuthLoadingError ("Failed to log out. Please try again." ));
84+ }
85+ }
86+
11987 String _extractErrorMessage (dynamic e) {
12088 if (e is Exception ) {
12189 return e.toString ().replaceAll ("Exception:" , "" ).trim ();
0 commit comments