storage cookie from shared preferences
This commit is contained in:
parent
fa90ba8c21
commit
463aa47a52
@ -1,10 +1,11 @@
|
|||||||
// ignore_for_file: unnecessary_brace_in_string_interps
|
// ignore_for_file: unnecessary_brace_in_string_interps
|
||||||
|
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'dart:convert';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -57,40 +58,13 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
String listUser = "";
|
String listUser = "";
|
||||||
|
|
||||||
Future<void> _incrementCounter() async {
|
Future<void> _incrementCounter() async {
|
||||||
var url = Uri.parse("http://localhost:8083/api/token");
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
String credentials = "peter93:toto";
|
var user = prefs.getString("user");
|
||||||
Codec<String, String> stringToBase64 = utf8.fuse(base64);
|
var jwt = prefs.getString("jwt");
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String former = "";
|
String former = "";
|
||||||
stderr.writeln("User ${user}");
|
stderr.writeln("User ${user}");
|
||||||
stderr.writeln("Jwt ${jwt}");
|
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 urlGet = Uri.parse("http://localhost:8083/api/users");
|
||||||
|
|
||||||
var responseGet = await http
|
var responseGet = await http
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'MyHomePage.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'MyHomePage.dart';
|
||||||
|
|
||||||
import 'classes/alert.dart';
|
import 'classes/alert.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -42,6 +45,30 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if ((response.statusCode == 200) || (response.statusCode == 201)) {
|
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(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user