1
1
import 'package:equatable/equatable.dart' ;
2
2
import 'package:flutter_bloc/flutter_bloc.dart' ;
3
+ import 'package:nc_cookbook_api/nc_cookbook_api.dart' ;
3
4
import 'package:nextcloud_cookbook_flutter/src/models/app_authentication.dart' ;
4
5
import 'package:nextcloud_cookbook_flutter/src/services/services.dart' ;
5
6
6
7
part 'authentication_event.dart' ;
8
+ part 'authentication_exception.dart' ;
7
9
part 'authentication_state.dart' ;
8
10
9
11
class AuthenticationBloc
10
12
extends Bloc <AuthenticationEvent , AuthenticationState > {
11
- AuthenticationBloc () : super (AuthenticationState ()) {
13
+ AuthenticationBloc () : super (const AuthenticationState ()) {
12
14
on < AppStarted > (_mapAppStartedEventToState);
13
15
on < LoggedIn > (_mapLoggedInEventToState);
14
16
on < LoggedOut > (_mapLoggedOutEventToState);
@@ -19,19 +21,28 @@ class AuthenticationBloc
19
21
AppStarted event,
20
22
Emitter <AuthenticationState > emit,
21
23
) async {
22
- final hasToken = await userRepository.hasAppAuthentication ();
23
-
24
- if (hasToken) {
25
- await userRepository.loadAppAuthentication ();
24
+ if (userRepository.hasAuthentidation) {
26
25
try {
26
+ await userRepository.loadAppAuthentication ();
27
+
27
28
final validCredentials = await userRepository.checkAppAuthentication ();
28
29
29
30
if (validCredentials) {
30
- emit (AuthenticationState (status: AuthenticationStatus .authenticated));
31
+ final apiVersion = await UserRepository ().fetchApiVersion ();
32
+ emit (
33
+ AuthenticationState (
34
+ status: AuthenticationStatus .authenticated,
35
+ apiVersion: apiVersion,
36
+ ),
37
+ );
31
38
} else {
32
39
await userRepository.deleteAppAuthentication ();
33
- emit (AuthenticationState (status: AuthenticationStatus .invalid));
40
+ emit (const AuthenticationState (status: AuthenticationStatus .invalid));
34
41
}
42
+ } on LoadAuthException {
43
+ emit (
44
+ const AuthenticationState (status: AuthenticationStatus .authenticated),
45
+ );
35
46
} catch (e) {
36
47
emit (
37
48
AuthenticationState (
@@ -41,25 +52,50 @@ class AuthenticationBloc
41
52
);
42
53
}
43
54
} else {
44
- emit (AuthenticationState (status: AuthenticationStatus .unauthenticated));
55
+ emit (
56
+ const AuthenticationState (
57
+ status: AuthenticationStatus .unauthenticated,
58
+ ),
59
+ );
45
60
}
46
61
}
47
62
48
63
Future <void > _mapLoggedInEventToState (
49
64
LoggedIn event,
50
65
Emitter <AuthenticationState > emit,
51
66
) async {
52
- emit (AuthenticationState ());
53
- await userRepository.persistAppAuthentication (event.appAuthentication);
54
- emit (AuthenticationState (status: AuthenticationStatus .authenticated));
67
+ emit (const AuthenticationState ());
68
+ try {
69
+ await userRepository.persistAppAuthentication (event.appAuthentication);
70
+
71
+ final apiVersion = await UserRepository ().fetchApiVersion ();
72
+ emit (
73
+ AuthenticationState (
74
+ status: AuthenticationStatus .authenticated,
75
+ apiVersion: apiVersion,
76
+ ),
77
+ );
78
+ } catch (e) {
79
+ emit (
80
+ AuthenticationState (
81
+ status: AuthenticationStatus .error,
82
+ error: e.toString (),
83
+ ),
84
+ );
85
+ }
55
86
}
56
87
57
88
Future <void > _mapLoggedOutEventToState (
58
89
LoggedOut event,
59
90
Emitter <AuthenticationState > emit,
60
91
) async {
61
- emit (AuthenticationState ());
62
- await userRepository.deleteAppAuthentication ();
63
- emit (AuthenticationState (status: AuthenticationStatus .unauthenticated));
92
+ emit (const AuthenticationState ());
93
+ try {
94
+ await userRepository.deleteAppAuthentication ();
95
+ } finally {
96
+ emit (
97
+ const AuthenticationState (status: AuthenticationStatus .unauthenticated),
98
+ );
99
+ }
64
100
}
65
101
}
0 commit comments