Aitrainer_app 1.1.0

Login and Registraion
This commit is contained in:
Bossanyi Tibor
2020-06-12 21:34:15 +02:00
parent 2c10480ffd
commit c8f0ced24d
41 changed files with 612 additions and 768 deletions
+69 -7
View File
@@ -1,12 +1,19 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:aitrainer_app/model/auth.dart';
class APIClient {
final String _baseUrl = 'http://andio.eu:8888/api/';
class APIClient extends ChangeNotifier {
Future<String> get(String endPoint, String param) async {
final url = _baseUrl + endPoint + param;
final response = await http.get(url, headers: {'Content-Type': 'application/json'});
final url = Auth.getBaseUrl() + endPoint + param;
Auth auth = Auth();
final response = await http.get(url,
headers: {
'Content-Type': 'application/json',
'Authorization' : "Bearer " + auth.getAuthToken() }
);
notifyListeners();
if(response.statusCode == 200) {
return utf8.decode(response.bodyBytes);
} else {
@@ -15,14 +22,69 @@ class APIClient {
}
Future<String> post(String endPoint, String body) async {
final url = _baseUrl + endPoint;
print(" ------------ http/post endpoint $endPoint body $body");
final url = Auth.getBaseUrl() + endPoint;
print(" ------------ http/post endpoint $endPoint body $body - url: $url ");
Auth auth = Auth();
final response = await http.post(url,
headers: {'Content-Type': 'application/json'},
headers: {
'Content-Type': 'application/json',
'Authorization' : "Bearer " + auth.getAuthToken()
},
body: body
);
print(" ------------ response: " + response.body.toString());
notifyListeners();
return response.body;
}
static dynamic authenticateUser(String email, String password) async {
var uri = Auth.getBaseUrl() + "authenticate";
try {
final body = '{"username":"$email", "password":"$password"}';
final response = await http.post(
uri,
headers: {
'Authorization': '1',
'Content-Type': 'application/json'
},
body: body
);
final responseCode = response.statusCode;
if ( responseCode != 200) {
return { "error" : "Authentication error, total failure", };
}
final responseJson = json.decode(response.body);
return responseJson;
} catch (exception) {
return { "error" : "Network error, try again later"};
}
}
static fetch(var authToken, var endPoint) async {
var uri = Auth.getBaseUrl() + endPoint;
try {
final response = await http.get(
uri,
headers: {
'Authorization': authToken
},
);
final responseJson = json.decode(response.body);
return responseJson;
} catch (exception) {
print(exception);
if(exception.toString().contains('SocketException')) {
return 'NetworkError';
} else {
return null;
}
}
}
}
+36 -1
View File
@@ -1,7 +1,8 @@
import 'dart:convert';
import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/model/user.dart';
import 'package:aitrainer_app/service/api.dart';
import 'package:aitrainer_app/model/auth.dart';
class CustomerApi {
final APIClient _client=new APIClient();
@@ -29,4 +30,38 @@ class CustomerApi {
"customers",
body);
}
Future<void> addUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== register new user: " + body );
final String responseBody = await _client.post(
"registration",
body);
Auth auth = Auth();
Customer customer = Customer.fromJson(jsonDecode(responseBody));
auth.afterRegistration(customer);
}
Future<void> getUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== login the user: " + body );
final String responseBody = await _client.post(
"login",
body);
Auth auth = Auth();
Customer customer = Customer.fromJson(jsonDecode(responseBody));
auth.afterRegistration(customer);
}
Future<void> getCustomer(int customerId) async {
String body = "";
print(" ===== get the customer by id: " + customerId.toString() );
final String responseBody = await _client.get(
"customers/"+customerId.toString(),
body);
Auth auth = Auth();
Customer customer = Customer.fromJson(jsonDecode(responseBody));
auth.afterRegistration(customer);
}
}