astronote/lib/providers/sign_in.dart
2020-07-31 17:30:32 +02:00

53 lines
1.6 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
class SignIn with ChangeNotifier {
String cookie;
Future<void> login(String url, String email, String password) async {
String urlApi = url + '/api/v1/login';
Map<String, String> headers = {
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
};
var body = json.encode({"login": email, "password": password});
final response = await http.post(urlApi, headers: headers, body: body);
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
print(response.headers['set-cookie']);
cookie = response.headers['set-cookie'];
//TODO : return http.get + company_choice_screen root
recoverUserData(urlApi);
// If the server did not return a 200 OK response,
// then throw an exception.
//TODO: error message ?
}
// final responseJson = json.decode(response.body);
}
Future<void> recoverUserData(String url) async {
Map<String, String> headers = {"X-Requested-With": "XMLHttpRequest","Cookie":cookie};
final response = await http.get(url, headers: headers);
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
print(response.headers['datas']);
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
}
// final responseJson = json.decode(response.body);
//
// print(responseJson);
}
}