Merge pull request 'feature/login' (#1) from feature/login into main

Reviewed-on: #1
This commit is contained in:
v4l3n71n 2024-06-23 15:18:07 +02:00
commit 672fb17904
5 changed files with 39 additions and 38 deletions

View File

@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app') project.evaluationDependsOn(':app')
} }
task clean(type: Delete) { tasks.register("clean", Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }

View File

@ -62,22 +62,21 @@ class _MyHomePageState extends State<MyHomePage> with ShowErrorDialog {
Future<void> _incrementCounter() async { Future<void> _incrementCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
var user = prefs.getString("user") ?? ""; var accessToken = prefs.getString("access_token") ?? "";
var jwt = prefs.getString("jwt") ?? "";
String former = ""; String former = "";
if ((user.isNotEmpty) && (jwt.isNotEmpty)) { if (accessToken.isNotEmpty) {
var urlGet = Uri.parse("http://${globals.api}/users"); var urlGet = Uri.parse("http://${globals.api}/events");
var responseGet = await http
.get(urlGet, headers: {HttpHeaders.cookieHeader: '${jwt}; ${user}'});
var responseGet = await http.get(urlGet,
headers: {HttpHeaders.cookieHeader: 'access_token=${accessToken}'});
stderr.writeln('Response Get status: ${responseGet.statusCode}'); stderr.writeln('Response Get status: ${responseGet.statusCode}');
if (responseGet.statusCode == 200) { if (responseGet.statusCode == 200) {
stderr.writeln('Response Get body: ${responseGet.body}'); stderr.writeln('Username : ${responseGet.body}');
var json = jsonDecode(responseGet.body); var json = jsonDecode(utf8.decode(responseGet.bodyBytes));
for (var user in json) { for (var user in json) {
former = "$former\n ${user['name']}"; stderr.writeln('name : ${user['name']}');
former = "$former\n ${user['name']} : ${user["place"]}";
} }
} else { } else {
var text = ""; var text = "";
@ -192,7 +191,7 @@ class _MyHomePageState extends State<MyHomePage> with ShowErrorDialog {
), ),
Text( Text(
'$listUser', '$listUser',
style: Theme.of(context).textTheme, style: Theme.of(context).textTheme.headlineMedium,
), ),
], ],
), ),

View File

@ -12,7 +12,6 @@ mixin ShowErrorDialog<T extends StatefulWidget> on State<T> {
ElevatedButton( ElevatedButton(
child: Text("OK"), child: Text("OK"),
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
primary: Colors.red,
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20), padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
textStyle: textStyle:
TextStyle(fontSize: 15, fontWeight: FontWeight.normal)), TextStyle(fontSize: 15, fontWeight: FontWeight.normal)),

View File

@ -39,11 +39,20 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
var password = inputPassword.text; var password = inputPassword.text;
if ((pseudo.isNotEmpty) && (password.isNotEmpty)) { if ((pseudo.isNotEmpty) && (password.isNotEmpty)) {
try { try {
String credentials = "${pseudo}:${password}"; //String credentials = "${pseudo}:${password}";
Codec<String, String> stringToBase64 = utf8.fuse(base64); //Codec<String, String> stringToBase64 = utf8.fuse(base64);
String encoded = stringToBase64.encode(credentials); //String encoded = stringToBase64.encode(credentials);
var response = await http.get(url, headers: { var response = await http.post(url,
HttpHeaders.authorizationHeader: 'Basic $encoded', // headers: {
// HttpHeaders.authorizationHeader: 'Basic $encoded',
//}
headers: {
'accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
"username": "${pseudo}",
"password": "${password}"
}); });
if ((response.statusCode == 200) || (response.statusCode == 201)) { if ((response.statusCode == 200) || (response.statusCode == 201)) {
@ -54,14 +63,9 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
var cookiesMany = cookie.split(","); var cookiesMany = cookie.split(",");
for (var cookie2 in cookiesMany) { for (var cookie2 in cookiesMany) {
switch (cookie2.split("=")[0]) { switch (cookie2.split("=")[0]) {
case "jwt": case "access_token":
{ {
prefs.setString("jwt", cookie2.toString()); prefs.setString("access_token", cookie2.split("=")[1]);
}
break;
case "user":
{
prefs.setString("user", cookie2.toString());
} }
break; break;
default: default:
@ -124,21 +128,19 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
void start() async { void start() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
var jwt = prefs.getString("jwt") ?? ""; var access_token = prefs.getString("access_token") ?? "";
var user = prefs.getString("user") ?? ""; if (access_token.isNotEmpty) {
if ((jwt.isNotEmpty) && (user.isNotEmpty)) { var urlToken = Uri.parse("https://${globals.api}/token");
var urlToken = Uri.parse("http://${globals.api}/token");
var responseToken = await http.get(urlToken, var responseToken = await http.get(urlToken,
headers: {HttpHeaders.cookieHeader: '${jwt}; ${user}'}); headers: {HttpHeaders.cookieHeader: 'access_token: ${access_token}'});
if (responseToken.statusCode == 200) { if (responseToken.statusCode == 200) {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (_) => MyHomePage(title: 'Flutter Demo'))); builder: (_) => MyHomePage(title: 'Flutter Demo')));
} else { } else {
prefs.remove("jwt"); prefs.remove("access_token");
prefs.remove("user");
} }
} }
} }
@ -168,7 +170,8 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
/*decoration: BoxDecoration( /*decoration: BoxDecoration(
color: Colors.red, color: Colors.red,
borderRadius: BorderRadius.circular(50.0)),*/ borderRadius: BorderRadius.circular(50.0)),*/
child: Image.asset('asset/images/flutter-logo.png')), //child: Image.asset('asset/images/flutter-logo.png')
),
), ),
), ),
Padding( Padding(

View File

@ -1 +1 @@
String api = "10.0.2.2:8083/api"; String api = "backend.valczeryba.ovh";