@@ -2,7 +2,6 @@ import 'dart:async';
2
2
3
3
import 'package:bloc/bloc.dart' ;
4
4
import 'package:equatable/equatable.dart' ;
5
- import 'package:ht_authentication_firebase/ht_authentication_firebase.dart' ;
6
5
import 'package:ht_authentication_repository/ht_authentication_repository.dart' ;
7
6
8
7
part 'authentication_event.dart' ;
@@ -18,9 +17,14 @@ class AuthenticationBloc
18
17
required HtAuthenticationRepository authenticationRepository,
19
18
}) : _authenticationRepository = authenticationRepository,
20
19
super (AuthenticationInitial ()) {
21
- on < AuthenticationEmailSignInRequested > (
22
- _onAuthenticationEmailSignInRequested,
20
+ // Register new event handlers
21
+ on < AuthenticationSendSignInLinkRequested > (
22
+ _onAuthenticationSendSignInLinkRequested,
23
23
);
24
+ on < AuthenticationSignInWithLinkAttempted > (
25
+ _onAuthenticationSignInWithLinkAttempted,
26
+ );
27
+ // Keep existing handlers
24
28
on < AuthenticationGoogleSignInRequested > (
25
29
_onAuthenticationGoogleSignInRequested,
26
30
);
@@ -35,22 +39,62 @@ class AuthenticationBloc
35
39
36
40
final HtAuthenticationRepository _authenticationRepository;
37
41
38
- /// Handles [AuthenticationEmailSignInRequested ] events.
39
- Future <void > _onAuthenticationEmailSignInRequested (
40
- AuthenticationEmailSignInRequested event,
42
+ /// Handles [AuthenticationSendSignInLinkRequested ] events.
43
+ Future <void > _onAuthenticationSendSignInLinkRequested (
44
+ AuthenticationSendSignInLinkRequested event,
41
45
Emitter <AuthenticationState > emit,
42
46
) async {
43
- emit (AuthenticationLoading ());
47
+ // Validate email format (basic check)
48
+ if (event.email.isEmpty || ! event.email.contains ('@' )) {
49
+ emit (const AuthenticationFailure ('Please enter a valid email address.' ));
50
+ return ;
51
+ }
52
+ emit (AuthenticationLinkSending ()); // Indicate link sending
44
53
try {
45
- await _authenticationRepository.signInWithEmailAndPassword (
54
+ await _authenticationRepository.sendSignInLinkToEmail (email: event.email);
55
+ emit (AuthenticationLinkSentSuccess ()); // Confirm link sent
56
+ } on SendSignInLinkException catch (e) {
57
+ emit (AuthenticationFailure ('Failed to send link: ${e .error }' ));
58
+ } catch (e) {
59
+ // Catch any other unexpected errors
60
+ emit (
61
+ AuthenticationFailure (
62
+ 'An unexpected error occurred: $e ' ,
63
+ ),
64
+ );
65
+ // Optionally log the stackTrace here
66
+ }
67
+ }
68
+
69
+ /// Handles [AuthenticationSignInWithLinkAttempted] events.
70
+ /// This assumes the event is dispatched after the app receives the deep link.
71
+ Future <void > _onAuthenticationSignInWithLinkAttempted (
72
+ AuthenticationSignInWithLinkAttempted event,
73
+ Emitter <AuthenticationState > emit,
74
+ ) async {
75
+ emit (AuthenticationLoading ()); // General loading for sign-in attempt
76
+ try {
77
+ await _authenticationRepository.signInWithEmailLink (
46
78
email: event.email,
47
- password : event.password ,
79
+ emailLink : event.emailLink ,
48
80
);
81
+ // On success, AppBloc should react to the user stream change from the repo.
82
+ // Resetting to Initial state here.
49
83
emit (AuthenticationInitial ());
50
- } on EmailSignInException catch (e) {
51
- emit (AuthenticationFailure (e.toString ()));
84
+ } on InvalidSignInLinkException catch (e) {
85
+ emit (
86
+ AuthenticationFailure (
87
+ 'Sign in failed: Invalid or expired link. ${e .error }' ,
88
+ ),
89
+ );
52
90
} catch (e) {
53
- emit (AuthenticationFailure (e.toString ()));
91
+ // Catch any other unexpected errors
92
+ emit (
93
+ AuthenticationFailure (
94
+ 'An unexpected error occurred during sign in: $e ' ,
95
+ ),
96
+ );
97
+ // Optionally log the stackTrace here
54
98
}
55
99
}
56
100
0 commit comments