Skip to content

Commit 8730da9

Browse files
committed
refactor(auth): Simplify authentication flow
- Remove user stream subscription - Emit initial state after auth events
1 parent 42831b1 commit 8730da9

File tree

6 files changed

+77
-84
lines changed

6 files changed

+77
-84
lines changed

lib/authentication/bloc/authentication_bloc.dart

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ class AuthenticationBloc
3131
on<AuthenticationDeleteAccountRequested>(
3232
_onAuthenticationDeleteAccountRequested,
3333
);
34-
35-
_userSubscription = _authenticationRepository.user.listen(
36-
(user) => add(AuthenticationUserChanged(user)),
37-
);
3834
}
3935

4036
final HtAuthenticationRepository _authenticationRepository;
41-
late final StreamSubscription<User> _userSubscription;
4237

4338
/// Handles [AuthenticationEmailSignInRequested] events.
4439
Future<void> _onAuthenticationEmailSignInRequested(
@@ -51,6 +46,7 @@ class AuthenticationBloc
5146
email: event.email,
5247
password: event.password,
5348
);
49+
emit(AuthenticationInitial());
5450
} on EmailSignInException catch (e) {
5551
emit(AuthenticationFailure(e.toString()));
5652
} catch (e) {
@@ -66,6 +62,7 @@ class AuthenticationBloc
6662
emit(AuthenticationLoading());
6763
try {
6864
await _authenticationRepository.signInWithGoogle();
65+
emit(AuthenticationInitial());
6966
} on GoogleSignInException catch (e) {
7067
emit(AuthenticationFailure(e.toString()));
7168
} catch (e) {
@@ -81,6 +78,7 @@ class AuthenticationBloc
8178
emit(AuthenticationLoading());
8279
try {
8380
await _authenticationRepository.signInAnonymously();
81+
emit(AuthenticationInitial());
8482
} on AnonymousLoginException catch (e) {
8583
emit(AuthenticationFailure(e.toString()));
8684
} catch (e) {
@@ -96,6 +94,7 @@ class AuthenticationBloc
9694
emit(AuthenticationLoading());
9795
try {
9896
await _authenticationRepository.signOut();
97+
emit(AuthenticationInitial());
9998
} on LogoutException catch (e) {
10099
emit(AuthenticationFailure(e.toString()));
101100
} catch (e) {
@@ -110,16 +109,11 @@ class AuthenticationBloc
110109
emit(AuthenticationLoading());
111110
try {
112111
await _authenticationRepository.deleteAccount();
112+
emit(AuthenticationInitial());
113113
} on DeleteAccountException catch (e) {
114114
emit(AuthenticationFailure(e.toString()));
115115
} catch (e) {
116116
emit(AuthenticationFailure(e.toString()));
117117
}
118118
}
119-
120-
@override
121-
Future<void> close() {
122-
_userSubscription.cancel();
123-
return super.close();
124-
}
125119
}

lib/authentication/bloc/authentication_event.dart

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,6 @@ sealed class AuthenticationEvent extends Equatable {
1111
List<Object> get props => [];
1212
}
1313

14-
/// {@template authentication_user_changed}
15-
/// Event triggered when the user's authentication state changes.
16-
/// {@endtemplate}
17-
final class AuthenticationUserChanged extends AuthenticationEvent {
18-
/// {@macro authentication_user_changed}
19-
const AuthenticationUserChanged(this.user);
20-
21-
/// The updated [User] object.
22-
final User user;
23-
24-
@override
25-
List<Object> get props => [user];
26-
}
27-
2814
/// {@template authentication_email_sign_in_requested}
2915
/// Event triggered when the user requests to sign in with email and password.
3016
/// {@endtemplate}

lib/authentication/view/authentication_page.dart

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -42,66 +42,68 @@ class _AuthenticationView extends StatelessWidget {
4242
}
4343
return Padding(
4444
padding: const EdgeInsets.all(16),
45-
child: Column(
46-
mainAxisAlignment: MainAxisAlignment.center,
47-
children: [
48-
const Text(
49-
'Sign In',
50-
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
51-
),
52-
const SizedBox(height: 32),
53-
TextFormField(
54-
controller: _emailController,
55-
decoration: const InputDecoration(
56-
labelText: 'Email',
57-
border: OutlineInputBorder(),
45+
child: SingleChildScrollView(
46+
child: Column(
47+
mainAxisAlignment: MainAxisAlignment.center,
48+
children: [
49+
const Text(
50+
'Sign In',
51+
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
5852
),
59-
keyboardType: TextInputType.emailAddress,
60-
),
61-
const SizedBox(height: 16),
62-
TextFormField(
63-
controller: _passwordController,
64-
decoration: const InputDecoration(
65-
labelText: 'Password',
66-
border: OutlineInputBorder(),
53+
const SizedBox(height: 32),
54+
TextFormField(
55+
controller: _emailController,
56+
decoration: const InputDecoration(
57+
labelText: 'Email',
58+
border: OutlineInputBorder(),
59+
),
60+
keyboardType: TextInputType.emailAddress,
6761
),
68-
obscureText: true,
69-
),
70-
const SizedBox(height: 32),
71-
ElevatedButton(
72-
onPressed: () {
73-
context.read<AuthenticationBloc>().add(
74-
AuthenticationEmailSignInRequested(
75-
email: _emailController.text,
76-
password: _passwordController.text,
77-
),
78-
);
79-
},
80-
child: const Text('Sign In with Email'),
81-
),
82-
const SizedBox(height: 16),
83-
ElevatedButton(
84-
onPressed: () {
85-
context
86-
.read<AuthenticationBloc>()
87-
.add(const AuthenticationGoogleSignInRequested());
88-
},
89-
style: ElevatedButton.styleFrom(
90-
backgroundColor: Colors.white,
91-
foregroundColor: Colors.black,
62+
const SizedBox(height: 16),
63+
TextFormField(
64+
controller: _passwordController,
65+
decoration: const InputDecoration(
66+
labelText: 'Password',
67+
border: OutlineInputBorder(),
68+
),
69+
obscureText: true,
9270
),
93-
child: const Text('Sign In with Google'),
94-
),
95-
const SizedBox(height: 16),
96-
ElevatedButton(
97-
onPressed: () {
98-
context
99-
.read<AuthenticationBloc>()
100-
.add(const AuthenticationAnonymousSignInRequested());
101-
},
102-
child: const Text('Sign In Anonymously'),
103-
),
104-
],
71+
const SizedBox(height: 32),
72+
ElevatedButton(
73+
onPressed: () {
74+
context.read<AuthenticationBloc>().add(
75+
AuthenticationEmailSignInRequested(
76+
email: _emailController.text,
77+
password: _passwordController.text,
78+
),
79+
);
80+
},
81+
child: const Text('Sign In with Email'),
82+
),
83+
const SizedBox(height: 16),
84+
ElevatedButton(
85+
onPressed: () {
86+
context
87+
.read<AuthenticationBloc>()
88+
.add(const AuthenticationGoogleSignInRequested());
89+
},
90+
style: ElevatedButton.styleFrom(
91+
backgroundColor: Colors.white,
92+
foregroundColor: Colors.black,
93+
),
94+
child: const Text('Sign In with Google'),
95+
),
96+
const SizedBox(height: 16),
97+
ElevatedButton(
98+
onPressed: () {
99+
context
100+
.read<AuthenticationBloc>()
101+
.add(const AuthenticationAnonymousSignInRequested());
102+
},
103+
child: const Text('Sign In Anonymously'),
104+
),
105+
],
106+
),
105107
),
106108
);
107109
},

lib/router/router.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final appRouter = GoRouter(
2020
if (appStatus == AppStatus.authenticated ||
2121
appStatus == AppStatus.anonymous) {
2222
if (!state.matchedLocation.startsWith(headlinesFeedPath)) {
23-
return headlinesFeedPath;
23+
return Routes.headlinesFeed;
2424
}
2525
}
2626
// If the user is not authenticated, redirect to the authentication page
@@ -97,5 +97,14 @@ final appRouter = GoRouter(
9797
),
9898
],
9999
),
100+
GoRoute(
101+
path: '/test-route',
102+
name: 'testRoute',
103+
builder: (BuildContext context, GoRouterState state) {
104+
return const Placeholder(
105+
child: Center(child: Text('Test Route')),
106+
);
107+
},
108+
),
100109
],
101110
);

lib/router/routes.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ abstract final class Routes {
1616
static const resetPasswordName = 'resetPassword';
1717
static const confirmEmail = 'confirm-email';
1818
static const confirmEmailName = 'confirmEmail';
19+
static const testRoute = '/test-route';
20+
static const testRouteName = 'testRoute';
1921

2022
static String getRouteNameByIndex(int index) {
2123
switch (index) {

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ packages:
451451
description:
452452
path: "."
453453
ref: main
454-
resolved-ref: "2c711c1ac23f4f3c866bea9d1152b83c188cf7fa"
454+
resolved-ref: "96250f7f8f57063915a593f5911ce0d6849a8280"
455455
url: "https://github.com/headlines-toolkit/ht-authentication-firebase.git"
456456
source: git
457457
version: "0.0.0"

0 commit comments

Comments
 (0)