add page password forgot

This commit is contained in:
Valentin CZERYBA 2025-02-14 23:39:44 +01:00
parent 6fdb5cb2fe
commit b0477c889c
2 changed files with 164 additions and 2 deletions

View File

@ -8,7 +8,7 @@ import 'dart:io';
//import 'MyHomePage.dart';
import 'pages/ListItemMenu.dart';
import 'pages/AddProfile.dart';
import 'pages/ForgotPassword.dart';
import 'classes/alert.dart';
import 'variable/globals.dart' as globals;
@ -221,7 +221,8 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
),
TextButton(
onPressed: () {
//TODO FORGOT PASSWORD SCREEN GOES HERE
Navigator.push(context,
MaterialPageRoute(builder: (_) => PasswordForgot()));
},
child: Text(
'Forgot Password',

View File

@ -0,0 +1,161 @@
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';
import 'dart:convert';
import 'dart:io';
import '../main.dart';
import '../classes/alert.dart';
import '../variable/globals.dart' as globals;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: PasswordForgot(),
);
}
}
class PasswordForgot extends StatefulWidget {
const PasswordForgot({super.key});
@override
_PasswordForgotState createState() => _PasswordForgotState();
}
class _PasswordForgotState extends State<PasswordForgot> with ShowAlertDialog {
TextEditingController inputEmail = TextEditingController();
convertNulltoEmptyString(var check) {
if (check == null) {
return "";
}
return check;
}
convertNulltoArray(List<String> check) {
if (check == null) {
return [];
}
return check;
}
String formatDate(String date) {
var splitedDate = date.split("/");
var day = splitedDate[0];
var month = splitedDate[1];
var year = splitedDate[2];
return "${year}-${month}-${day}";
}
Future<void> _forgotPassword(BuildContext context) async {
var email = inputEmail.text;
var urlPut = Uri.parse("${globals.api}/password/forgot");
var responsePost = await http.post(urlPut,
headers: {
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
HttpHeaders.contentTypeHeader: 'application/json'
},
body: jsonEncode({
'email': email,
}));
print(responsePost.statusCode);
if (responsePost.statusCode == 200) {
showAlertDialog(context, "Creation", "Un email a été envoyé à ${email}");
sleep(Duration(seconds: 5));
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (_) => LoginDemo()));
return;
}
final errorMessages = {
400: "Requête mal construite",
406: "Mot de passe incorrect",
404: "Utilisateur inconnu",
403: "Utilisateur désactivé",
410: "Token invalide",
500: "Problème interne du serveur",
};
final text = errorMessages[responsePost.statusCode] ??
"Problème d'authentification inconnu";
showAlertDialog(context, "Erreur serveur", text);
}
@override
void initState() {
super.initState();
}
final _formKey = GlobalKey<FormState>();
String? _validateField(String? value) {
return value!.isEmpty ? 'Champ requis' : null;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text("Create profile"),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextFormField(
controller: inputEmail,
validator: (value) => _validateField(value),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Email',
hintText: 'Modifier l\'adresse mail'),
),
),
SizedBox(
height: 30,
),
Container(
height: 50,
width: 250,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(20)),
child: TextButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_forgotPassword(context);
}
},
child: Text(
'Envoyer le mail de reinitialisation',
style: TextStyle(color: Colors.white, fontSize: 25),
),
),
)
],
),
),
));
}
}