wt1.1.2f fix for 1.1.3

This commit is contained in:
Bossanyi Tibor
2020-10-28 14:05:31 +01:00
parent 27c1d26dfd
commit db95c9a6f7
26 changed files with 672 additions and 510 deletions
+26 -1
View File
@@ -25,6 +25,14 @@ class CustomerApi {
body);
}
Future<void> updateFirebaseUid(int customerId, String uid) async {
print(" ===== update Firebase uid : " + customerId.toString() + ": " +
uid);
await _client.post(
"customers/update_firebase_uid/" + customerId.toString(),
uid);
}
Future<void> addCustomer(Customer customer) async {
String body = JsonEncoder().convert(customer.toJson());
print(" ===== add new customer: " + body);
@@ -35,7 +43,7 @@ class CustomerApi {
Future<void> addUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== register new user: " + body);
print(" ===== add new user: " + body);
final String responseBody = await _client.post(
"registration",
body);
@@ -68,6 +76,23 @@ class CustomerApi {
}
}
Future<void> getUserByEmail(String email) async {
print(" ===== User getByEmail : " + email);
final String responseBody = await _client.get(
"customers/find_by_email/" + email,
"");
Customer customer;
try {
customer = Customer.fromJson(jsonDecode(responseBody));
if ( customer.firebaseUid == null ) {
await this.updateFirebaseUid(customer.customerId, Cache().firebaseUid);
}
Cache().userLoggedIn = customer;
} on FormatException {
throw new Exception(responseBody);
}
}
Future<void> getCustomer(int customerId) async {
String body = "";
+105
View File
@@ -0,0 +1,105 @@
import 'package:aitrainer_app/model/cache.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
class FirebaseApi {
static FirebaseApi _instance;
static final FirebaseAuth auth = FirebaseAuth.instance;
static const String SIGN_IN_OK = "OK";
static const String SIGN_IN_NOT_FOUND = "user-not-found";
static const String SIGN_IN_WRONG_PWD = "wrong-password";
static const String REGISTER_WEAK_PWD = "weak-password";
static const String REGISTER_EMAIL_IN_USE = "email-already-in-use";
UserCredential userCredential;
factory FirebaseApi() => _instance ?? FirebaseApi._internal();
FirebaseApi._internal() {
_instance = this;
}
// Define an async function to initialize FlutterFire
Future<void> initializeFlutterFire() async {
try {
// Wait for Firebase to initialize and set `_initialized` state to true
await Firebase.initializeApp();
} catch (e) {
// Set `_error` state to true if Firebase initialization fails
print("Error initializing Firebase");
}
}
Future<String> signInEmail(String email, String password) async {
String rc = SIGN_IN_OK;
try {
userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password
);
Cache().firebaseUid = userCredential.user.uid;
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No user found for that email.');
rc = SIGN_IN_NOT_FOUND;
} else if (e.code == 'wrong-password') {
print('Wrong password provided for that user.');
rc = SIGN_IN_WRONG_PWD;
throw Exception("Customer does not exist or the password is wrong");
}
return e.code;
}
return rc;
}
Future<String> registerEmail(String email, String password) async {
String rc = SIGN_IN_OK;
try {
userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password
);
Cache().firebaseUid = userCredential.user.uid;
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
rc = REGISTER_WEAK_PWD;
throw Exception("Password too short");
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
throw Exception("Customer exists");
rc = REGISTER_EMAIL_IN_USE;
}
} catch (e) {
print(e);
throw Exception(e.toString());
}
return rc;
}
Future<UserCredential> signInWithFacebook() async {
// Trigger the sign-in flow
final LoginResult result = await FacebookAuth.instance.login();
// Create a credential from the access token
final FacebookAuthCredential facebookAuthCredential =
FacebookAuthProvider.credential(result.accessToken.token);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
}
Future<void> signOut() async {
await FirebaseAuth.instance.signOut();
}
Future<void> resetPassword(String email) async {
await FirebaseAuth.instance.sendPasswordResetEmail(email: email);
}
}