90 lines
2.7 KiB
Dart
90 lines
2.7 KiB
Dart
import 'dart:convert';
|
|
import 'package:path/path.dart';
|
|
import 'package:sqflite/sqflite.dart';
|
|
import 'package:astronote_app/models/user_datas.dart';
|
|
import 'package:astronote_app/models/credential.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class LogIn with ChangeNotifier {
|
|
String _cookie;
|
|
Credential _userCredentials;
|
|
|
|
bool get isLog {
|
|
return token != null;
|
|
}
|
|
|
|
String get token {
|
|
if (_cookie != null) {
|
|
return _cookie;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> logIn(String url, String login, 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": login, "password": password});
|
|
|
|
final response = await http.post(urlApi, headers: headers, body: body);
|
|
if (response.statusCode == 200) {
|
|
// If the server did return a 20 0 OK response,
|
|
_cookie = response.headers['set-cookie'];
|
|
_userCredentials = new Credential(url: url, login: login, password: password);
|
|
print(_userCredentials.toString());
|
|
// await OpenLocalBdd();
|
|
return recoverUserDatas(urlApi);
|
|
} else{
|
|
_cookie = null;
|
|
_userCredentials = null;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> recoverUserDatas(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.
|
|
final responseJson = json.decode(response.body);
|
|
|
|
User userDatas = new User.fromJson(responseJson['datas']);
|
|
print(userDatas.companies[0].toString());
|
|
}
|
|
}
|
|
|
|
// Future<Database> OpenLocalBdd() async {
|
|
// // Open the database and store the reference.
|
|
// final Future<Database> database = openDatabase(
|
|
// // Set the path to the database. Note: Using the `join` function from the
|
|
// // `path` package is best practice to ensure the path is correctly
|
|
// // constructed for each platform.
|
|
// join(await getDatabasesPath(), 'astronote_database.db'),
|
|
// onCreate: (db, version) {
|
|
// // Run the CREATE TABLE statement on the database.
|
|
// return db.execute(
|
|
// "CREATE TABLE credentials(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)",
|
|
// );
|
|
// },
|
|
// // Set the version. This executes the onCreate function and provides a
|
|
// // path to perform database upgrades and downgrades.
|
|
// version: 1,
|
|
//
|
|
// );
|
|
// print(await getDatabasesPath());
|
|
// await database;
|
|
// }
|
|
|
|
|
|
}
|