add profile wip
This commit is contained in:
parent
6a3ec9a969
commit
90e61cebbf
@ -7,6 +7,7 @@ import 'dart:io';
|
||||
|
||||
//import 'MyHomePage.dart';
|
||||
import 'pages/ListItemMenu.dart';
|
||||
import 'pages/AddProfile.dart';
|
||||
|
||||
import 'classes/alert.dart';
|
||||
|
||||
@ -36,100 +37,73 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
|
||||
TextEditingController inputPseudo = TextEditingController();
|
||||
TextEditingController inputPassword = TextEditingController();
|
||||
Future<void> _login(BuildContext context) async {
|
||||
var url = Uri.parse("${globals.api}/token");
|
||||
var pseudo = inputPseudo.text;
|
||||
var password = inputPassword.text;
|
||||
print("get login");
|
||||
print(pseudo.isNotEmpty);
|
||||
print(password.isNotEmpty);
|
||||
if ((pseudo.isNotEmpty) && (password.isNotEmpty)) {
|
||||
print(url);
|
||||
try {
|
||||
//String credentials = "${pseudo}:${password}";
|
||||
//Codec<String, String> stringToBase64 = utf8.fuse(base64);
|
||||
//String encoded = stringToBase64.encode(credentials);
|
||||
var response = await http.post(url,
|
||||
// headers: {
|
||||
// HttpHeaders.authorizationHeader: 'Basic $encoded',
|
||||
//}
|
||||
headers: {
|
||||
'accept': 'application/json',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: {
|
||||
"username": "${pseudo}",
|
||||
"password": "${password}"
|
||||
});
|
||||
print(response.statusCode);
|
||||
if ((response.statusCode == 200) || (response.statusCode == 201)) {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
final url = Uri.parse("${globals.api}/token");
|
||||
final pseudo = inputPseudo.text;
|
||||
final password = inputPassword.text;
|
||||
|
||||
var cookies = response.headers["set-cookie"].toString().split(";");
|
||||
for (var cookie in cookies) {
|
||||
var cookiesMany = cookie.split(",");
|
||||
for (var cookie2 in cookiesMany) {
|
||||
switch (cookie2.split("=")[0]) {
|
||||
case "access_token":
|
||||
{
|
||||
prefs.setString("access_token", cookie2.split("=")[1]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
print("Attempting login");
|
||||
if (pseudo.isEmpty || password.isEmpty) {
|
||||
showAlertDialog(context, "Erreur", "Champ vide");
|
||||
return;
|
||||
}
|
||||
|
||||
print("Request URL: $url");
|
||||
try {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {
|
||||
'accept': 'application/json',
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
body: {
|
||||
"username": pseudo,
|
||||
"password": password,
|
||||
},
|
||||
);
|
||||
|
||||
print("Response status code: ${response.statusCode}");
|
||||
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final cookies = response.headers["set-cookie"]?.split(";") ?? [];
|
||||
|
||||
for (final cookie in cookies) {
|
||||
final cookieParts = cookie.split(",");
|
||||
for (final part in cookieParts) {
|
||||
final keyValue = part.split("=");
|
||||
if (keyValue.length == 2 && keyValue[0] == "access_token") {
|
||||
prefs.setString("access_token", keyValue[1]);
|
||||
}
|
||||
}
|
||||
Navigator.push(
|
||||
context, MaterialPageRoute(builder: (_) => ListItemMenu()));
|
||||
} else {
|
||||
var text = "";
|
||||
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) {
|
||||
showAlertDialog(context, "Erreur", "${e}");
|
||||
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => ListItemMenu()),
|
||||
);
|
||||
} else {
|
||||
_handleErrorResponse(context, response.statusCode);
|
||||
}
|
||||
} else {
|
||||
showAlertDialog(context, "Erreur", "Champ vide");
|
||||
} catch (e) {
|
||||
showAlertDialog(context, "Erreur", e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var access_token = prefs.getString("access_token") ?? "";
|
||||
@ -272,7 +246,12 @@ class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
|
||||
SizedBox(
|
||||
height: 130,
|
||||
),
|
||||
Text('New User? Create Account')
|
||||
InkWell(
|
||||
child: Text('New User? Create Account'),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context, MaterialPageRoute(builder: (_) => AddProfile()));
|
||||
})
|
||||
],
|
||||
),
|
||||
),
|
||||
|
@ -32,8 +32,7 @@ class AddProfile extends StatefulWidget {
|
||||
_AddProfileState createState() => _AddProfileState();
|
||||
}
|
||||
|
||||
class _AddProfileState extends State<AddProfile>
|
||||
with ShowAlertDialog {
|
||||
class _AddProfileState extends State<AddProfile> with ShowAlertDialog {
|
||||
TextEditingController inputUserName = TextEditingController();
|
||||
|
||||
TextEditingController inputName = TextEditingController();
|
||||
@ -80,7 +79,7 @@ class _AddProfileState extends State<AddProfile>
|
||||
return "${year}-${month}-${day}";
|
||||
}
|
||||
|
||||
Future<void> _updateProfile(BuildContext context) async {
|
||||
Future<void> _createProfile(BuildContext context) async {
|
||||
var username = inputUserName.text;
|
||||
var firstName = inputFirstName.text;
|
||||
var name = inputName.text;
|
||||
@ -98,44 +97,42 @@ class _AddProfileState extends State<AddProfile>
|
||||
|
||||
var urlPut = Uri.parse("${globals.api}/users");
|
||||
|
||||
var responsePost = await http.post(urlPut,
|
||||
headers: {
|
||||
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
|
||||
HttpHeaders.contentTypeHeader: 'application/json'
|
||||
},
|
||||
body: jsonEncode({
|
||||
'name': name,
|
||||
'username': username,
|
||||
'firstName': firstName,
|
||||
'password': password,
|
||||
'email': email,
|
||||
'roles': '',
|
||||
'birth': birth.toString()
|
||||
}));
|
||||
print(responsePost.statusCode);
|
||||
if (responsePost.statusCode == 200) {
|
||||
showAlertDialog(context, "Ajout", "Votre utilisateur a été ajouté");
|
||||
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);
|
||||
var responsePost = await http.post(urlPut,
|
||||
headers: {
|
||||
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
|
||||
HttpHeaders.contentTypeHeader: 'application/json'
|
||||
},
|
||||
body: jsonEncode({
|
||||
'name': name,
|
||||
'username': username,
|
||||
'firstName': firstName,
|
||||
'password': password,
|
||||
'email': email,
|
||||
'roles': '',
|
||||
'birth': birth.toString()
|
||||
}));
|
||||
print(responsePost.statusCode);
|
||||
if (responsePost.statusCode == 200) {
|
||||
showAlertDialog(context, "Ajout", "Votre utilisateur a été ajouté");
|
||||
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();
|
||||
@ -266,11 +263,11 @@ class _AddProfileState extends State<AddProfile>
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_updateProfile(context);
|
||||
_createProfile(context);
|
||||
}
|
||||
},
|
||||
child: Text(
|
||||
'Modifier le profil',
|
||||
'Créer le profil',
|
||||
style: TextStyle(color: Colors.white, fontSize: 25),
|
||||
),
|
||||
),
|
||||
|
Loading…
x
Reference in New Issue
Block a user