2022-08-27 12:14:34 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-17 22:41:21 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-09-04 20:09:09 +02:00
|
|
|
import 'package:http/http.dart' as http;
|
2022-09-17 22:41:21 +02:00
|
|
|
|
2022-09-04 20:09:09 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
2022-09-17 22:41:21 +02:00
|
|
|
|
2024-06-23 17:23:26 +02:00
|
|
|
//import 'MyHomePage.dart';
|
2024-06-30 15:17:10 +02:00
|
|
|
import 'pages/ListItemMenu.dart';
|
2025-01-22 23:33:09 +01:00
|
|
|
import 'pages/AddProfile.dart';
|
2022-09-17 22:41:21 +02:00
|
|
|
|
2022-09-13 00:11:43 +02:00
|
|
|
import 'classes/alert.dart';
|
2022-09-04 20:09:09 +02:00
|
|
|
|
2022-09-21 23:00:37 +02:00
|
|
|
import 'variable/globals.dart' as globals;
|
2024-11-06 15:55:15 +01:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2022-09-21 23:00:37 +02:00
|
|
|
|
2022-08-27 12:14:34 +02:00
|
|
|
void main() {
|
2022-09-03 21:17:47 +02:00
|
|
|
runApp(MyApp());
|
2022-08-27 12:14:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
2022-09-03 21:17:47 +02:00
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
home: LoginDemo(),
|
2022-08-27 12:14:34 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:17:47 +02:00
|
|
|
class LoginDemo extends StatefulWidget {
|
2022-08-27 12:14:34 +02:00
|
|
|
@override
|
2022-09-03 21:17:47 +02:00
|
|
|
_LoginDemoState createState() => _LoginDemoState();
|
2022-08-27 12:14:34 +02:00
|
|
|
}
|
|
|
|
|
2024-12-30 22:34:17 +01:00
|
|
|
class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
|
2022-09-06 22:02:37 +02:00
|
|
|
TextEditingController inputPseudo = TextEditingController();
|
|
|
|
TextEditingController inputPassword = TextEditingController();
|
|
|
|
Future<void> _login(BuildContext context) async {
|
2025-01-22 23:33:09 +01:00
|
|
|
final url = Uri.parse("${globals.api}/token");
|
|
|
|
final pseudo = inputPseudo.text;
|
|
|
|
final password = inputPassword.text;
|
2022-09-17 22:41:21 +02:00
|
|
|
|
2025-01-22 23:33:09 +01:00
|
|
|
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]);
|
2022-09-17 22:41:21 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-07 23:15:54 +02:00
|
|
|
}
|
2025-01-22 23:33:09 +01:00
|
|
|
|
|
|
|
Navigator.push(
|
|
|
|
context,
|
|
|
|
MaterialPageRoute(builder: (_) => ListItemMenu()),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
_handleErrorResponse(context, response.statusCode);
|
2022-09-06 22:02:37 +02:00
|
|
|
}
|
2025-01-22 23:33:09 +01:00
|
|
|
} catch (e) {
|
|
|
|
showAlertDialog(context, "Erreur", e.toString());
|
2022-09-04 20:09:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-22 23:33:09 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-09-17 23:01:11 +02:00
|
|
|
void start() async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2024-06-23 14:28:03 +02:00
|
|
|
var access_token = prefs.getString("access_token") ?? "";
|
2024-06-30 13:32:36 +02:00
|
|
|
print("Get access token");
|
|
|
|
|
2024-06-23 14:28:03 +02:00
|
|
|
if (access_token.isNotEmpty) {
|
2024-06-30 13:32:36 +02:00
|
|
|
print("Appel HTTP");
|
2024-06-30 16:27:24 +02:00
|
|
|
var urlToken = Uri.parse("${globals.api}/token");
|
2022-09-18 22:44:15 +02:00
|
|
|
|
|
|
|
var responseToken = await http.get(urlToken,
|
2024-06-23 14:28:03 +02:00
|
|
|
headers: {HttpHeaders.cookieHeader: 'access_token: ${access_token}'});
|
2024-06-30 13:32:36 +02:00
|
|
|
print(responseToken.statusCode);
|
2022-09-18 22:44:15 +02:00
|
|
|
if (responseToken.statusCode == 200) {
|
2024-06-30 13:32:36 +02:00
|
|
|
print("route to item list");
|
2022-09-20 00:31:11 +02:00
|
|
|
Navigator.push(
|
2024-06-23 17:23:26 +02:00
|
|
|
context, MaterialPageRoute(builder: (_) => ListItemMenu()));
|
2022-09-18 22:44:15 +02:00
|
|
|
} else {
|
2024-06-23 14:28:03 +02:00
|
|
|
prefs.remove("access_token");
|
2022-09-18 22:44:15 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-17 23:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2024-11-06 15:55:15 +01:00
|
|
|
_checkLocationPermission();
|
2022-09-17 23:01:11 +02:00
|
|
|
start();
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-11-06 15:55:15 +01:00
|
|
|
Future<void> _checkLocationPermission() async {
|
|
|
|
PermissionStatus status = await Permission.location.status;
|
|
|
|
|
|
|
|
if (status.isGranted) {
|
|
|
|
print("Location permission granted");
|
|
|
|
} else if (status.isDenied) {
|
|
|
|
print("Location permission denied");
|
|
|
|
_requestLocationPermission();
|
|
|
|
} else if (status.isPermanentlyDenied) {
|
|
|
|
print("Location permission permanently denied");
|
|
|
|
openAppSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request location permission
|
|
|
|
Future<void> _requestLocationPermission() async {
|
|
|
|
PermissionStatus status = await Permission.location.request();
|
|
|
|
|
|
|
|
if (status.isGranted) {
|
|
|
|
print("Location permission granted");
|
|
|
|
} else if (status.isDenied) {
|
|
|
|
print("Location permission denied");
|
|
|
|
} else if (status.isPermanentlyDenied) {
|
|
|
|
print("Location permission permanently denied");
|
|
|
|
openAppSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open app settings to allow user to grant permission manually
|
|
|
|
Future<void> _openAppSettings() async {
|
|
|
|
bool opened = await openAppSettings();
|
|
|
|
if (opened) {
|
|
|
|
print("App settings opened");
|
|
|
|
} else {
|
|
|
|
print("Failed to open app settings");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 12:14:34 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2022-09-03 21:17:47 +02:00
|
|
|
backgroundColor: Colors.white,
|
2022-08-27 12:14:34 +02:00
|
|
|
appBar: AppBar(
|
2022-09-03 21:17:47 +02:00
|
|
|
title: Text("Login Page"),
|
2024-06-26 23:10:16 +02:00
|
|
|
backgroundColor: Colors.blue,
|
|
|
|
foregroundColor: Colors.white,
|
2022-08-27 12:14:34 +02:00
|
|
|
),
|
2022-09-03 21:17:47 +02:00
|
|
|
body: SingleChildScrollView(
|
2022-08-27 12:14:34 +02:00
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
2022-09-03 21:17:47 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 60.0),
|
|
|
|
child: Center(
|
|
|
|
child: Container(
|
2024-07-04 23:17:44 +02:00
|
|
|
width: 200,
|
|
|
|
height: 150,
|
|
|
|
decoration: BoxDecoration(
|
2022-09-03 21:17:47 +02:00
|
|
|
color: Colors.red,
|
2024-07-04 23:17:44 +02:00
|
|
|
borderRadius: BorderRadius.circular(50.0)),
|
|
|
|
child: Image.asset('./images/flutter.png')),
|
2022-09-03 21:17:47 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 15),
|
|
|
|
child: TextField(
|
2022-09-06 22:02:37 +02:00
|
|
|
controller: inputPseudo,
|
2022-09-03 21:17:47 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
2022-09-13 00:15:16 +02:00
|
|
|
labelText: 'Pseudo',
|
|
|
|
hintText: 'Enter pseudo existent'),
|
2022-09-01 22:32:35 +02:00
|
|
|
),
|
|
|
|
),
|
2022-09-03 21:17:47 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
|
|
|
//padding: EdgeInsets.symmetric(horizontal: 15),
|
|
|
|
child: TextField(
|
2022-09-06 22:02:37 +02:00
|
|
|
controller: inputPassword,
|
2022-09-03 21:17:47 +02:00
|
|
|
obscureText: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
labelText: 'Password',
|
|
|
|
hintText: 'Enter secure password'),
|
|
|
|
),
|
|
|
|
),
|
2024-06-19 23:24:42 +02:00
|
|
|
TextButton(
|
2022-09-03 21:17:47 +02:00
|
|
|
onPressed: () {
|
|
|
|
//TODO FORGOT PASSWORD SCREEN GOES HERE
|
|
|
|
},
|
|
|
|
child: Text(
|
|
|
|
'Forgot Password',
|
|
|
|
style: TextStyle(color: Colors.blue, fontSize: 15),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
height: 50,
|
|
|
|
width: 250,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.blue, borderRadius: BorderRadius.circular(20)),
|
2024-06-19 23:24:42 +02:00
|
|
|
child: TextButton(
|
2022-09-03 21:17:47 +02:00
|
|
|
onPressed: () {
|
2022-09-06 22:02:37 +02:00
|
|
|
_login(context);
|
2022-09-03 21:17:47 +02:00
|
|
|
},
|
|
|
|
child: Text(
|
|
|
|
'Login',
|
|
|
|
style: TextStyle(color: Colors.white, fontSize: 25),
|
|
|
|
),
|
|
|
|
),
|
2022-08-27 12:14:34 +02:00
|
|
|
),
|
2022-09-03 21:17:47 +02:00
|
|
|
SizedBox(
|
|
|
|
height: 130,
|
2022-08-27 12:14:34 +02:00
|
|
|
),
|
2025-01-22 23:33:09 +01:00
|
|
|
InkWell(
|
|
|
|
child: Text('New User? Create Account'),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.push(
|
|
|
|
context, MaterialPageRoute(builder: (_) => AddProfile()));
|
|
|
|
})
|
2022-08-27 12:14:34 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|