This commit is contained in:
Valentin CZERYBA 2024-06-23 14:28:03 +02:00
parent d439217f1d
commit dac570fffc
2 changed files with 16 additions and 18 deletions

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}/users");
var responseGet = await http var responseGet = await http.get(urlGet,
.get(urlGet, headers: {HttpHeaders.cookieHeader: '${jwt}; ${user}'}); 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(responseGet.body);
for (var user in json) { for (var user in json) {
former = "$former\n ${user['name']}"; stderr.writeln('email : ${user['email']}');
former = "$former\n ${user['email']}";
} }
} else { } else {
var text = ""; var text = "";

View File

@ -128,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("https://${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");
} }
} }
} }
@ -167,12 +165,13 @@ class _LoginDemoState extends State<LoginDemo> with ShowErrorDialog {
padding: const EdgeInsets.only(top: 60.0), padding: const EdgeInsets.only(top: 60.0),
child: Center( child: Center(
child: Container( child: Container(
width: 200, width: 200,
height: 150, height: 150,
/*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(