add profile wip

This commit is contained in:
Valentin CZERYBA 2025-01-22 23:33:09 +01:00
parent 6a3ec9a969
commit 90e61cebbf
2 changed files with 103 additions and 127 deletions

View File

@ -7,6 +7,7 @@ import 'dart:io';
//import 'MyHomePage.dart'; //import 'MyHomePage.dart';
import 'pages/ListItemMenu.dart'; import 'pages/ListItemMenu.dart';
import 'pages/AddProfile.dart';
import 'classes/alert.dart'; import 'classes/alert.dart';
@ -36,98 +37,71 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
TextEditingController inputPseudo = TextEditingController(); TextEditingController inputPseudo = TextEditingController();
TextEditingController inputPassword = TextEditingController(); TextEditingController inputPassword = TextEditingController();
Future<void> _login(BuildContext context) async { Future<void> _login(BuildContext context) async {
var url = Uri.parse("${globals.api}/token"); final url = Uri.parse("${globals.api}/token");
var pseudo = inputPseudo.text; final pseudo = inputPseudo.text;
var password = inputPassword.text; final password = inputPassword.text;
print("get login");
print(pseudo.isNotEmpty); print("Attempting login");
print(password.isNotEmpty); if (pseudo.isEmpty || password.isEmpty) {
if ((pseudo.isNotEmpty) && (password.isNotEmpty)) { showAlertDialog(context, "Erreur", "Champ vide");
print(url); return;
}
print("Request URL: $url");
try { try {
//String credentials = "${pseudo}:${password}"; final response = await http.post(
//Codec<String, String> stringToBase64 = utf8.fuse(base64); url,
//String encoded = stringToBase64.encode(credentials);
var response = await http.post(url,
// headers: {
// HttpHeaders.authorizationHeader: 'Basic $encoded',
//}
headers: { headers: {
'accept': 'application/json', 'accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
}, },
body: { body: {
"username": "${pseudo}", "username": pseudo,
"password": "${password}" "password": password,
}); },
print(response.statusCode); );
if ((response.statusCode == 200) || (response.statusCode == 201)) {
SharedPreferences prefs = await SharedPreferences.getInstance();
var cookies = response.headers["set-cookie"].toString().split(";"); print("Response status code: ${response.statusCode}");
for (var cookie in cookies) {
var cookiesMany = cookie.split(","); if (response.statusCode == 200 || response.statusCode == 201) {
for (var cookie2 in cookiesMany) { final prefs = await SharedPreferences.getInstance();
switch (cookie2.split("=")[0]) { final cookies = response.headers["set-cookie"]?.split(";") ?? [];
case "access_token":
{ for (final cookie in cookies) {
prefs.setString("access_token", cookie2.split("=")[1]); final cookieParts = cookie.split(",");
} for (final part in cookieParts) {
break; final keyValue = part.split("=");
default: if (keyValue.length == 2 && keyValue[0] == "access_token") {
break; prefs.setString("access_token", keyValue[1]);
} }
} }
} }
Navigator.push( Navigator.push(
context, MaterialPageRoute(builder: (_) => ListItemMenu())); context,
MaterialPageRoute(builder: (_) => ListItemMenu()),
);
} else { } else {
var text = ""; _handleErrorResponse(context, response.statusCode);
switch (response.statusCode) {
case 400:
{
text = "Requête mal construite";
}
break;
case 406:
{
text = "Mot de passe incorrect";
}
break;
case 404:
{
text = "Utilisateur inconnu";
}
break;
case 403:
{
text = "Utilisateur desactive";
}
break;
case 410:
{
text = "Token invalide";
}
break;
case 500:
{
text = "Probleme interne du serveur";
}
break;
default:
{
text = "Probleme d'authentification inconnu";
}
break;
}
showAlertDialog(context, "Erreur serveur", text);
} }
} catch (e) { } catch (e) {
showAlertDialog(context, "Erreur", "${e}"); showAlertDialog(context, "Erreur", e.toString());
} }
} else {
showAlertDialog(context, "Erreur", "Champ vide");
} }
void _handleErrorResponse(BuildContext context, int statusCode) {
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 errorMessage =
errorMessages[statusCode] ?? "Problème d'authentification inconnu";
showAlertDialog(context, "Erreur serveur", errorMessage);
} }
void start() async { void start() async {
@ -272,7 +246,12 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
SizedBox( SizedBox(
height: 130, height: 130,
), ),
Text('New User? Create Account') InkWell(
child: Text('New User? Create Account'),
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (_) => AddProfile()));
})
], ],
), ),
), ),

View File

@ -32,8 +32,7 @@ class AddProfile extends StatefulWidget {
_AddProfileState createState() => _AddProfileState(); _AddProfileState createState() => _AddProfileState();
} }
class _AddProfileState extends State<AddProfile> class _AddProfileState extends State<AddProfile> with ShowAlertDialog {
with ShowAlertDialog {
TextEditingController inputUserName = TextEditingController(); TextEditingController inputUserName = TextEditingController();
TextEditingController inputName = TextEditingController(); TextEditingController inputName = TextEditingController();
@ -80,7 +79,7 @@ class _AddProfileState extends State<AddProfile>
return "${year}-${month}-${day}"; return "${year}-${month}-${day}";
} }
Future<void> _updateProfile(BuildContext context) async { Future<void> _createProfile(BuildContext context) async {
var username = inputUserName.text; var username = inputUserName.text;
var firstName = inputFirstName.text; var firstName = inputFirstName.text;
var name = inputName.text; var name = inputName.text;
@ -133,8 +132,6 @@ class _AddProfileState extends State<AddProfile>
"Problème d'authentification inconnu"; "Problème d'authentification inconnu";
showAlertDialog(context, "Erreur serveur", text); showAlertDialog(context, "Erreur serveur", text);
} }
}
@override @override
void initState() { void initState() {
@ -266,11 +263,11 @@ class _AddProfileState extends State<AddProfile>
child: TextButton( child: TextButton(
onPressed: () { onPressed: () {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
_updateProfile(context); _createProfile(context);
} }
}, },
child: Text( child: Text(
'Modifier le profil', 'Créer le profil',
style: TextStyle(color: Colors.white, fontSize: 25), style: TextStyle(color: Colors.white, fontSize: 25),
), ),
), ),