Skip to content

Commit 7da1efd

Browse files
authored
Merge pull request #84 from PDG-NUTRI/81-auth-guard
81 auth guard
2 parents 8a8b70b + 4431bab commit 7da1efd

10 files changed

+154
-28
lines changed

lib/main.dart

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
12
import 'package:flutter/material.dart';
23
import 'package:firebase_core/firebase_core.dart';
4+
import 'package:get_it/get_it.dart';
5+
import 'package:pdg_app/provider/auth_provider.dart';
6+
import 'package:pdg_app/router/auth_gard.dart';
37
import 'package:pdg_app/router/router.gr.dart';
48
import 'package:pdg_app/theme.dart';
9+
import 'api/firebase_client.dart';
10+
import 'api/firebase_connection.dart';
511
import 'firebase_options.dart';
612

13+
Future<void> setup() async {
14+
final getIt = GetIt.instance;
15+
16+
getIt.registerSingleton<AuthProvider>(
17+
AuthProvider(
18+
auth: FirebaseConnection(),
19+
clientApi: FirebaseClient(FirebaseFirestore.instance),
20+
),
21+
);
22+
}
23+
724
void main() async {
825
WidgetsFlutterBinding.ensureInitialized();
926
await Firebase.initializeApp(
1027
options: DefaultFirebaseOptions.currentPlatform,
1128
);
29+
await setup();
1230
runApp(MyApp());
1331
}
1432

1533
class MyApp extends StatelessWidget {
1634
MyApp({Key? key}) : super(key: key);
1735

18-
final _appRouter = AppRouter();
36+
final _appRouter = AppRouter(authGuard: AuthGuard());
1937

2038
@override
2139
Widget build(BuildContext context) {

lib/provider/auth_provider.dart

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:pdg_app/api/iauth.dart';
3+
import 'package:pdg_app/api/iclient.dart';
4+
5+
import '../model/client.dart';
6+
7+
class AuthProvider extends ChangeNotifier {
8+
final Auth _auth;
9+
final IClient _clientApi;
10+
11+
AuthProvider({required Auth auth, required IClient clientApi})
12+
: _auth = auth,
13+
_clientApi = clientApi;
14+
15+
Client? _client;
16+
17+
bool isConnected() {
18+
return _auth.isConnected;
19+
}
20+
21+
String get userUid => _auth.uid;
22+
23+
Client? get client => _client;
24+
25+
Future<void> signIn(String email, String password) async {
26+
//final isConnected = await _auth.signIn(email: email, password: password);
27+
final isConnected =
28+
await _auth.signIn(email: "[email protected]", password: 'crepes');
29+
30+
if (isConnected) {
31+
_client = await _clientApi.readClient(_auth.uid);
32+
notifyListeners();
33+
}
34+
}
35+
36+
Future<void> signOut() async {
37+
await _auth.signOut();
38+
_client = null;
39+
notifyListeners();
40+
}
41+
42+
Future<void> register(String email, String password) async {
43+
await _auth.register(email: email, password: password);
44+
}
45+
}

lib/router/auth_gard.dart

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
import 'package:auto_route/auto_route.dart';
2+
import 'package:get_it/get_it.dart';
3+
import 'package:pdg_app/router/router.gr.dart';
24

3-
class AuthGard extends AutoRouteGuard {
4-
var isAuthenticated = false;
5+
import '../provider/auth_provider.dart';
56

7+
class AuthGuard extends AutoRouteGuard {
68
@override
79
void onNavigation(NavigationResolver resolver, StackRouter router) {
8-
//TODO à faire
9-
10-
// if (!isAuthenticated) {
11-
// router.pushAndPopUntil(
12-
// LoginScreenRoute(onLoginResult: (_) {
13-
// isAuthenticated = true;
14-
// // we can't pop the bottom page in the navigator's stack
15-
// // so we just remove it from our local stack
16-
// resolver.next();
17-
// router.removeLast();
18-
// }),
19-
// predicate: (r) => true,);
20-
// }
10+
if (GetIt.I.get<AuthProvider>().isConnected()) {
11+
resolver.next(true);
12+
} else {
13+
router.push(const LoginScreenRoute());
14+
}
2115
}
2216
}

lib/router/router.dart

+2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import 'package:pdg_app/screens/profile.dart';
88
import 'package:pdg_app/screens/register.dart';
99

1010
import '../screens/home.dart';
11+
import './auth_gard.dart';
1112

1213
@MaterialAutoRouter(
1314
// replaceInRouteName: 'Page,Route',
1415
routes: <AutoRoute>[
1516
AutoRoute(
1617
path: '/home',
1718
page: HomeScreen,
19+
guards: [AuthGuard],
1820
initial: true,
1921
children: [
2022
RedirectRoute(

lib/router/router.gr.dart

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ import '../screens/home.dart' as _i1;
2222
import '../screens/login.dart' as _i2;
2323
import '../screens/profile.dart' as _i6;
2424
import '../screens/register.dart' as _i3;
25+
import 'auth_gard.dart' as _i11;
2526

2627
class AppRouter extends _i9.RootStackRouter {
27-
AppRouter([_i10.GlobalKey<_i10.NavigatorState>? navigatorKey])
28+
AppRouter(
29+
{_i10.GlobalKey<_i10.NavigatorState>? navigatorKey,
30+
required this.authGuard})
2831
: super(navigatorKey);
2932

33+
final _i11.AuthGuard authGuard;
34+
3035
@override
3136
final Map<String, _i9.PageFactory> pagesMap = {
3237
HomeScreenRoute.name: (routeData) {
@@ -67,7 +72,9 @@ class AppRouter extends _i9.RootStackRouter {
6772
List<_i9.RouteConfig> get routes => [
6873
_i9.RouteConfig('/#redirect',
6974
path: '/', redirectTo: '/home', fullMatch: true),
70-
_i9.RouteConfig(HomeScreenRoute.name, path: '/home', children: [
75+
_i9.RouteConfig(HomeScreenRoute.name, path: '/home', guards: [
76+
authGuard
77+
], children: [
7178
_i9.RouteConfig('#redirect',
7279
path: '',
7380
parent: HomeScreenRoute.name,

lib/screens/login.dart

+41-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
11
import 'package:auto_route/auto_route.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:get_it/get_it.dart';
4+
import 'package:pdg_app/provider/auth_provider.dart';
35
import 'package:pdg_app/widgets/forms/main_text_field.dart';
46
import 'package:pdg_app/widgets/right_arrow_button.dart';
57

68
import '../router/router.gr.dart';
79
import '../widgets/login/bottom_shape.dart';
810
import '../widgets/login/top_shape.dart';
911

10-
class LoginScreen extends StatelessWidget {
12+
class LoginScreen extends StatefulWidget {
1113
const LoginScreen({Key? key}) : super(key: key);
1214

15+
@override
16+
State<LoginScreen> createState() => _LoginScreenState();
17+
}
18+
19+
class _LoginScreenState extends State<LoginScreen> {
20+
final TextEditingController _emailController = TextEditingController();
21+
final TextEditingController _passwordController = TextEditingController();
22+
1323
@override
1424
Widget build(BuildContext context) {
1525
return Login(
16-
onLoginPress: () =>
17-
AutoRouter.of(context).navigate(const AddMealScreenRoute()),
26+
emailController: _emailController,
27+
passwordController: _passwordController,
28+
onLoginPress: () async {
29+
final auth = GetIt.I.get<AuthProvider>();
30+
await auth.signIn(_emailController.text, _passwordController.text);
31+
if (auth.isConnected()) {
32+
// ignore: use_build_context_synchronously
33+
AutoRouter.of(context).navigate(const HomeScreenRoute());
34+
} else {
35+
// ignore: use_build_context_synchronously
36+
ScaffoldMessenger.of(context).showSnackBar(
37+
const SnackBar(
38+
content: Text('Login failed'),
39+
),
40+
);
41+
}
42+
},
1843
onRegisterPress: () =>
1944
AutoRouter.of(context).navigate(const RegisterScreenRoute()),
2045
);
@@ -25,13 +50,19 @@ class Login extends StatelessWidget {
2550
final void Function()? onLoginPress;
2651
final void Function()? onRegisterPress;
2752
final double screenWidth;
53+
final TextEditingController? _emailController;
54+
final TextEditingController? _passwordController;
2855

2956
const Login({
3057
this.onLoginPress,
3158
this.onRegisterPress,
3259
this.screenWidth = 0,
60+
TextEditingController? emailController,
61+
TextEditingController? passwordController,
3362
Key? key,
34-
}) : super(key: key);
63+
}) : _emailController = emailController,
64+
_passwordController = passwordController,
65+
super(key: key);
3566

3667
@override
3768
Widget build(BuildContext context) {
@@ -99,14 +130,16 @@ class Login extends StatelessWidget {
99130
],
100131
),
101132
const SizedBox(height: 40),
102-
const MainTextField(
133+
MainTextField(
134+
controller: _emailController,
103135
name: 'Email',
104-
icon: Icon(Icons.email_outlined),
136+
icon: const Icon(Icons.email_outlined),
105137
),
106138
const SizedBox(height: 20),
107-
const MainTextField(
139+
MainTextField(
140+
controller: _passwordController,
108141
name: 'Password',
109-
icon: Icon(Icons.lock_outlined),
142+
icon: const Icon(Icons.lock_outlined),
110143
obscureText: true,
111144
),
112145
const SizedBox(height: 40),

lib/screens/profile.dart

+13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import 'package:auto_route/auto_route.dart';
12
import 'package:flutter/material.dart';
3+
import 'package:get_it/get_it.dart';
24
import 'package:pdg_app/widgets/cards/left_element_card.dart';
35
import 'package:pdg_app/widgets/text_information.dart';
46
import 'package:intl/intl.dart';
7+
import '../router/router.gr.dart';
58
import '../widgets/profile/profile_top_bar.dart';
69

10+
import '../provider/auth_provider.dart';
11+
712
class ProfileScreen extends StatelessWidget {
813
const ProfileScreen({Key? key}) : super(key: key);
914

@@ -18,6 +23,10 @@ class ProfileScreen extends StatelessWidget {
1823
clientPhone: "0794563418",
1924
clientBirthday: DateTime(1996, 12, 18),
2025
clientInsurance: "09734789789248943",
26+
onLogoutPressed: () {
27+
GetIt.I.get<AuthProvider>().signOut();
28+
AutoRouter.of(context).navigate(const LoginScreenRoute());
29+
},
2130
);
2231
}
2332
}
@@ -33,6 +42,7 @@ class Profile extends StatelessWidget {
3342
final String _clientPhone;
3443
final DateTime _clientBirthday;
3544
final String _clientInsurance;
45+
final void Function()? _onLogoutPressed;
3646

3747
const Profile({
3848
screenWidth = 0.0,
@@ -45,6 +55,7 @@ class Profile extends StatelessWidget {
4555
required clientPhone,
4656
required clientBirthday,
4757
required clientInsurance,
58+
void Function()? onLogoutPressed,
4859
Key? key,
4960
}) : _screenWidth = screenWidth,
5061
_clientPicturePath = clientPicturePath,
@@ -56,6 +67,7 @@ class Profile extends StatelessWidget {
5667
_clientPhone = clientPhone,
5768
_clientBirthday = clientBirthday,
5869
_clientInsurance = clientInsurance,
70+
_onLogoutPressed = onLogoutPressed,
5971
super(key: key);
6072

6173
@override
@@ -75,6 +87,7 @@ class Profile extends StatelessWidget {
7587
clientPicturePath: _clientPicturePath,
7688
clientFirstName: _clientFirstName,
7789
clientLastName: _clientLastName,
90+
onLogOutPress: _onLogoutPressed,
7891
),
7992
const SizedBox(height: 15),
8093
Expanded(

lib/widgets/profile/profile_top_bar.dart

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ProfileTopBar extends StatelessWidget {
1010
final String _clientPicturePath;
1111
final String _clientFirstName;
1212
final String _clientLastName;
13+
final void Function()? _onLogOutPress;
1314

1415
const ProfileTopBar({
1516
Key? key,
@@ -18,11 +19,13 @@ class ProfileTopBar extends StatelessWidget {
1819
required String clientPicturePath,
1920
required String clientFirstName,
2021
required String clientLastName,
22+
void Function()? onLogOutPress,
2123
}) : _width = width,
2224
_height = height,
2325
_clientPicturePath = clientPicturePath,
2426
_clientFirstName = clientFirstName,
2527
_clientLastName = clientLastName,
28+
_onLogOutPress = onLogOutPress,
2629
super(key: key);
2730

2831
@override
@@ -56,8 +59,11 @@ class ProfileTopBar extends StatelessWidget {
5659
child: Column(mainAxisAlignment: MainAxisAlignment.start, children: [
5760
Row(
5861
mainAxisAlignment: MainAxisAlignment.end,
59-
children: const [
60-
CustomIconButton(icon: Icons.logout_outlined),
62+
children: [
63+
CustomIconButton(
64+
icon: Icons.logout_outlined,
65+
onTap: _onLogOutPress,
66+
),
6167
],
6268
),
6369
]),

pubspec.lock

+7
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,13 @@ packages:
443443
url: "https://pub.dartlang.org"
444444
source: hosted
445445
version: "2.1.3"
446+
get_it:
447+
dependency: "direct main"
448+
description:
449+
name: get_it
450+
url: "https://pub.dartlang.org"
451+
source: hosted
452+
version: "7.2.0"
446453
glob:
447454
dependency: transitive
448455
description:

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies:
4949
day_night_time_picker: ^1.1.2
5050
google_nav_bar: ^5.0.6
5151
path_provider: ^2.0.11
52+
get_it: ^7.2.0
5253

5354
dev_dependencies:
5455
flutter_test:

0 commit comments

Comments
 (0)