From 2fd191094e0884fd898b81cfe7591a94f04ed4fd Mon Sep 17 00:00:00 2001 From: Enzo Date: Fri, 10 Nov 2023 16:08:38 +0100 Subject: [PATCH] fonction de connection --- lib/Modele/Api/request_api.dart | 6 ++- lib/Modele/user.dart | 21 ++--------- lib/View/login/login_view.dart | 66 +++++++++++++++++++++++++++++++-- lib/View/page_test.dart | 2 +- lib/main.dart | 4 +- 5 files changed, 74 insertions(+), 25 deletions(-) diff --git a/lib/Modele/Api/request_api.dart b/lib/Modele/Api/request_api.dart index 643dc11..ba546ff 100644 --- a/lib/Modele/Api/request_api.dart +++ b/lib/Modele/Api/request_api.dart @@ -88,9 +88,13 @@ class RequestApi extends IDataStrategy { if (response.statusCode == 200) { Map json = jsonDecode(response.body); return Tuple2(true, json['token'].toString()); - } else if (response.statusCode == 401) { + } + if (response.statusCode == 401) { return const Tuple2(false, "UNAUTHORIZED"); } + if (response.statusCode == 404) { + return const Tuple2(false, "Not found"); + } return const Tuple2(false, "Fail"); } diff --git a/lib/Modele/user.dart b/lib/Modele/user.dart index a4303fb..e0adfcf 100644 --- a/lib/Modele/user.dart +++ b/lib/Modele/user.dart @@ -2,21 +2,8 @@ import 'package:flutter/material.dart'; import 'package:smartfit_app_mobile/Modele/activity.dart'; class User extends ChangeNotifier { - // A modifier - late String _username; - late String _email; - late String _passwordHash; - late List _listActivity; - - String get username => _username; - String get email => _email; - String get passwordHash => _passwordHash; - List get listActivity => _listActivity; - - User(String username, String email, String passwordHash) { - _username = username; - _email = email; - _passwordHash = passwordHash; - _listActivity = List.empty(growable: true); - } + String? username; + String? email; + String? token; + List? listActivity; } diff --git a/lib/View/login/login_view.dart b/lib/View/login/login_view.dart index 17fa1ce..335df80 100644 --- a/lib/View/login/login_view.dart +++ b/lib/View/login/login_view.dart @@ -1,8 +1,17 @@ +import 'dart:convert'; + +import 'package:crypto/crypto.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:provider/provider.dart'; +import 'package:smartfit_app_mobile/Modele/Api/i_data_strategy.dart'; +import 'package:smartfit_app_mobile/Modele/Api/request_api.dart'; +import 'package:smartfit_app_mobile/Modele/user.dart'; +import 'package:smartfit_app_mobile/View/page_test.dart'; import 'package:smartfit_app_mobile/common/colo_extension.dart'; import 'package:smartfit_app_mobile/common_widget/round_button.dart'; import 'package:smartfit_app_mobile/common_widget/round_text_field.dart'; import 'package:flutter/material.dart'; +import 'package:tuple/tuple.dart'; class LoginView extends StatefulWidget { const LoginView({super.key}); @@ -13,6 +22,32 @@ class LoginView extends StatefulWidget { class _LoginViewState extends State { bool isCheck = false; + IDataStrategy api = RequestApi(); + + final controllerTextEmail = TextEditingController(); + final controllerTextPassword = TextEditingController(); + + Future> checkLoginAndPassword() async { + Tuple2 result = await api.connexion(controllerTextEmail.text, + sha256.convert(utf8.encode(controllerTextPassword.text)).toString()); + return result; + } + + Future>> getUserInfo(String token) async { + Tuple2 result = await api.getInfoUser(token); + if (result.item1 == false) { + return const Tuple2(false, {"Empty": "Empty"}); + } + return Tuple2(true, result.item2 as Map); + } + + void fillUser(BuildContext context, Map map, String token) { + context.read().email = map["email"]; + context.read().username = map["username"]; + context.read().token = token; + context.read().listActivity = List.empty(growable: true); + } + @override Widget build(BuildContext context) { var media = MediaQuery.of(context).size; @@ -43,15 +78,17 @@ class _LoginViewState extends State { SizedBox( height: media.width * 0.04, ), - const RoundTextField( + RoundTextField( hitText: "Email", icon: "assets/img/email.svg", keyboardType: TextInputType.emailAddress, + controller: controllerTextEmail, ), SizedBox( height: media.width * 0.04, ), RoundTextField( + controller: controllerTextPassword, hitText: "Mot de passe", icon: "assets/img/lock.svg", obscureText: true, @@ -80,11 +117,32 @@ class _LoginViewState extends State { ), ], ), - const Spacer(), + const Spacer(), RoundButton( title: "Se connecter", - onPressed: () { + onPressed: () async { + Tuple2 result = + await checkLoginAndPassword(); + if (result.item1 == true) { + Tuple2 infoUser = await getUserInfo(result.item2); + + if (infoUser.item1 == false) { + print( + "Erreur - Impossible de récupéré les données de l'utilisateur"); + // Afficher pop-up + } else { + fillUser(context, infoUser.item2, result.item2); + + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => const TestPage())); + } + } else { + print("Connection refuser"); + //Afficher une pop-up + } }), SizedBox( height: media.width * 0.04, @@ -198,4 +256,4 @@ class _LoginViewState extends State { ), ); } -} \ No newline at end of file +} diff --git a/lib/View/page_test.dart b/lib/View/page_test.dart index e4c02c0..49e4cbe 100644 --- a/lib/View/page_test.dart +++ b/lib/View/page_test.dart @@ -179,7 +179,7 @@ class _TestPage extends State { body: Column( children: [ const Text('A random AWESOME idea:'), - Text(Provider.of(context).username), + const Text("User"), // ↓ Add this. ElevatedButton( diff --git a/lib/main.dart b/lib/main.dart index 1643aec..8e10bdd 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,14 +1,14 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:smartfit_app_mobile/Modele/user.dart'; +import 'package:smartfit_app_mobile/View/login/login_view.dart'; import 'package:smartfit_app_mobile/View/on_boarding/started_view.dart'; import 'package:smartfit_app_mobile/View/page_test.dart'; import 'package:smartfit_app_mobile/common/colo_extension.dart'; void main() { runApp(ChangeNotifierProvider( - create: (context) => User("toto", "toto@email", "1234"), - child: const MyApp())); + create: (context) => User(), child: const MyApp())); } class MyApp extends StatelessWidget {