Finish user log in to endi's account + rename some files and class
This commit is contained in:
89
lib/providers/log_in.dart
Normal file
89
lib/providers/log_in.dart
Normal file
@@ -0,0 +1,89 @@
|
||||
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;
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user