@@ -3,6 +3,9 @@ import 'package:bloc/bloc.dart';
3
3
import 'package:cloudmate/src/blocs/authentication/bloc.dart' ;
4
4
import 'package:cloudmate/src/configs/application.dart' ;
5
5
import 'package:cloudmate/src/models/user.dart' ;
6
+ import 'package:cloudmate/src/resources/local/user_local.dart' ;
7
+ import 'package:cloudmate/src/resources/remote/authentication_repository.dart' ;
8
+ import 'package:cloudmate/src/resources/remote/user_repository.dart' ;
6
9
import 'package:cloudmate/src/routes/app_pages.dart' ;
7
10
8
11
class AuthBloc extends Bloc <AuthEvent , AuthState > {
@@ -14,26 +17,83 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
14
17
@override
15
18
Stream <AuthState > mapEventToState (event) async * {
16
19
if (event is OnAuthCheck ) {
17
- yield await _onAuthCheck (event);
20
+ bool isLogined = await _onAuthCheck ();
21
+ if (isLogined) {
22
+ yield AuthenticationSuccess ();
23
+ } else {
24
+ yield AuthenticationFail ();
25
+ }
18
26
}
19
27
20
- if (event is OnAuthProcess ) {
21
- yield await _handlePressedLogin (event );
28
+ if (event is OnClear ) {
29
+ yield AuthenticationSuccess (userModel : userModel );
22
30
}
23
31
24
- if (event is OnClear ) {
25
- yield AuthenticationFail ();
32
+ if (event is LoginEvent ) {
33
+ bool isSuccess = await _handleLogin (event);
34
+ AppNavigator .pop ();
35
+ if (isSuccess) {
36
+ yield AuthenticationSuccess ();
37
+ } else {
38
+ yield AuthenticationFail ();
39
+ }
40
+ }
41
+
42
+ if (event is RegisterEvent ) {
43
+ bool isSuccess = await _handleRegister (event);
44
+ AppNavigator .pop ();
45
+ if (isSuccess) {
46
+ yield AuthenticationSuccess ();
47
+ } else {
48
+ yield AuthenticationFail ();
49
+ }
50
+ }
51
+
52
+ if (event is LogOutEvent ) {
53
+ bool isSuccess = await _handleLogOut ();
54
+ if (isSuccess) {
55
+ yield AuthenticationFail ();
56
+ }
57
+ }
58
+
59
+ if (event is GetInfoUser ) {
60
+ _handleGetUserInfo ();
61
+ yield AuthenticationSuccess ();
26
62
}
27
63
}
28
64
29
- Future <AuthState > _onAuthCheck (OnAuthCheck event) async {
30
- await Future .delayed (Duration (seconds: 1 ));
31
- return AuthenticationSuccess (userModel: userModel);
65
+ Future <bool > _onAuthCheck () async {
66
+ return UserLocal ().getAccessToken () != '' ;
67
+ }
68
+
69
+ Future <bool > _handleLogin (LoginEvent event) async {
70
+ bool isSuccess = await AuthenticationRepository ().login (
71
+ event.username,
72
+ event.password,
73
+ );
74
+
75
+ return isSuccess;
76
+ }
77
+
78
+ Future <bool > _handleRegister (RegisterEvent event) async {
79
+ bool isSuccess = await AuthenticationRepository ().register (
80
+ fistName: event.firstName,
81
+ lastName: event.lastName,
82
+ username: event.username,
83
+ password: event.password,
84
+ );
85
+
86
+ return isSuccess;
87
+ }
88
+
89
+ Future <bool > _handleLogOut () async {
90
+ await AuthenticationRepository ().logOut ();
91
+ return true ;
32
92
}
33
93
34
- Future <AuthState > _handlePressedLogin ( OnAuthProcess event ) async {
35
- print (event.username );
36
- AppNavigator . pop () ;
37
- return AuthenticationSuccess (userModel: userModel );
94
+ Future <void > _handleGetUserInfo ( ) async {
95
+ UserModel ? user = await UserRepository (). getInfoUser ( );
96
+ userModel = user ;
97
+ print (userModel. toString () );
38
98
}
39
99
}
0 commit comments