Skip to content

Commit cfac838

Browse files
committed
feat: Add routeings for the different FlutterFire authentication states
1 parent 6def415 commit cfac838

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

lib/app/routes/router_config.dart

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import 'package:community_app/counter/view/counter_page.dart';
2+
import 'package:firebase_auth/firebase_auth.dart'
3+
show ActionCodeSettings, FirebaseAuth;
4+
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
5+
import 'package:flutter/cupertino.dart';
6+
import 'package:go_router/go_router.dart';
7+
8+
final providers = <AuthProvider>[
9+
EmailAuthProvider(),
10+
EmailLinkAuthProvider(
11+
actionCodeSettings: ActionCodeSettings(
12+
url: 'http://localhost',
13+
handleCodeInApp: true,
14+
androidMinimumVersion: '1',
15+
androidPackageName: 'dev.flutterexplained.community_app.dev',
16+
iOSBundleId: 'dev.flutterexplained.community_app.dev',
17+
),
18+
),
19+
];
20+
21+
final GoRouter routerConfig = GoRouter(
22+
routes: <RouteBase>[
23+
GoRoute(
24+
path: '/',
25+
builder: (BuildContext context, GoRouterState state) {
26+
return FirebaseAuth.instance.currentUser != null
27+
? const CounterPage()
28+
: SignInScreen(
29+
providers: providers,
30+
actions: [
31+
EmailLinkSignInAction((context) {
32+
context.go('/email-link-sign-in');
33+
})
34+
],
35+
);
36+
},
37+
),
38+
GoRoute(
39+
path: '/sign-in',
40+
builder: (BuildContext context, GoRouterState state) {
41+
return SignInScreen(
42+
providers: providers,
43+
actions: [
44+
EmailLinkSignInAction((context) {
45+
context.go('/email-link-sign-in');
46+
}),
47+
AuthStateChangeAction((context, state) {
48+
context.go('/profile');
49+
})
50+
],
51+
);
52+
},
53+
),
54+
GoRoute(
55+
path: '/email-link-sign-in',
56+
builder: (context, state) => EmailLinkSignInScreen(
57+
actions: [
58+
AuthStateChangeAction((context, state) {
59+
context.go('/profile');
60+
})
61+
],
62+
),
63+
),
64+
GoRoute(
65+
path: '/profile',
66+
builder: (BuildContext context, GoRouterState state) {
67+
return ProfileScreen(
68+
providers: providers,
69+
actions: [
70+
SignedOutAction((context) {
71+
context.go('/sign-in');
72+
})
73+
],
74+
);
75+
},
76+
)
77+
],
78+
);

lib/app/routes/routes.dart

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export './router_config.dart';

lib/app/view/app.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:community_app/counter/counter.dart';
1+
import 'package:community_app/app/routes/router_config.dart';
22
import 'package:community_app/l10n/l10n.dart';
33
import 'package:flutter/material.dart';
44

@@ -7,7 +7,7 @@ class App extends StatelessWidget {
77

88
@override
99
Widget build(BuildContext context) {
10-
return MaterialApp(
10+
return MaterialApp.router(
1111
theme: ThemeData(
1212
appBarTheme: const AppBarTheme(color: Color(0xFF13B9FF)),
1313
colorScheme: ColorScheme.fromSwatch(
@@ -16,7 +16,7 @@ class App extends StatelessWidget {
1616
),
1717
localizationsDelegates: AppLocalizations.localizationsDelegates,
1818
supportedLocales: AppLocalizations.supportedLocales,
19-
home: const CounterPage(),
19+
routerConfig: routerConfig,
2020
);
2121
}
2222
}

0 commit comments

Comments
 (0)