From 463aa47a52ec5300e199d8f0d8772b9e206633fc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 17 Sep 2022 22:41:21 +0200 Subject: [PATCH] storage cookie from shared preferences --- covas_mobile/lib/MyHomePage.dart | 38 +++++--------------------------- covas_mobile/lib/main.dart | 31 ++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/covas_mobile/lib/MyHomePage.dart b/covas_mobile/lib/MyHomePage.dart index 3c01511..e3c8a3d 100644 --- a/covas_mobile/lib/MyHomePage.dart +++ b/covas_mobile/lib/MyHomePage.dart @@ -1,10 +1,11 @@ // ignore_for_file: unnecessary_brace_in_string_interps import 'dart:io'; +import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; -import 'dart:convert'; +import 'package:shared_preferences/shared_preferences.dart'; void main() { runApp(const MyApp()); @@ -57,40 +58,13 @@ class _MyHomePageState extends State { String listUser = ""; Future _incrementCounter() async { - var url = Uri.parse("http://localhost:8083/api/token"); - String credentials = "peter93:toto"; - Codec stringToBase64 = utf8.fuse(base64); - String encoded = stringToBase64.encode(credentials); - var response = await http.get(url, headers: { - HttpHeaders.authorizationHeader: 'Basic $encoded', - }); - var user = ""; - var jwt = ""; - 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 "jwt": - { - jwt = cookie2.toString(); - } - break; - case "user": - { - user = cookie2.toString(); - } - break; - default: - break; - } - } - } + SharedPreferences prefs = await SharedPreferences.getInstance(); + var user = prefs.getString("user"); + var jwt = prefs.getString("jwt"); String former = ""; stderr.writeln("User ${user}"); stderr.writeln("Jwt ${jwt}"); - stderr.writeln('Response status: ${response.statusCode}'); - stderr.writeln('Response body: ${response.body}'); + var urlGet = Uri.parse("http://localhost:8083/api/users"); var responseGet = await http diff --git a/covas_mobile/lib/main.dart b/covas_mobile/lib/main.dart index 5ac6aa0..6c67c62 100644 --- a/covas_mobile/lib/main.dart +++ b/covas_mobile/lib/main.dart @@ -1,9 +1,12 @@ import 'package:flutter/material.dart'; -import 'MyHomePage.dart'; - +import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; + import 'dart:convert'; import 'dart:io'; + +import 'MyHomePage.dart'; + import 'classes/alert.dart'; void main() { @@ -42,6 +45,30 @@ class _LoginDemoState extends State with ShowErrorDialog { }); if ((response.statusCode == 200) || (response.statusCode == 201)) { + SharedPreferences prefs = await SharedPreferences.getInstance(); + + var cookies = response.headers["set-cookie"].toString().split(";"); + prefs.remove("jwt"); + prefs.remove("user"); + for (var cookie in cookies) { + var cookiesMany = cookie.split(","); + for (var cookie2 in cookiesMany) { + switch (cookie2.split("=")[0]) { + case "jwt": + { + prefs.setString("jwt", cookie2); + } + break; + case "user": + { + prefs.setString("user", cookie2); + } + break; + default: + break; + } + } + } Navigator.push( context, MaterialPageRoute(