WT1.1.3 build 2 iOS. Apple SignIn, Google SignIn
This commit is contained in:
@@ -2,8 +2,12 @@ import 'package:aitrainer_app/model/cache.dart';
|
||||
import 'package:aitrainer_app/model/user.dart';
|
||||
import 'package:aitrainer_app/service/customer_service.dart';
|
||||
import 'package:aitrainer_app/service/firebase_api.dart';
|
||||
import 'package:aitrainer_app/service/logging.dart';
|
||||
import 'package:aitrainer_app/util/not_found_exception.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart' as auth;
|
||||
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
|
||||
|
||||
class UserRepository {
|
||||
class UserRepository with Logging {
|
||||
User user;
|
||||
|
||||
UserRepository() {
|
||||
@@ -31,29 +35,166 @@ class UserRepository {
|
||||
await CustomerApi().addUser(modelUser);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
log(e.toString());
|
||||
throw new Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addUserFB() async {
|
||||
final User modelUser = this.user;
|
||||
try {
|
||||
Map<String, dynamic> userData = await FirebaseApi().registerWithFacebook();
|
||||
if (userData != null) {
|
||||
modelUser.email = userData['email'];
|
||||
if (modelUser.email == null) {
|
||||
throw new Exception("Facebook signup was not successful. Please try another method");
|
||||
}
|
||||
modelUser.password = Cache().firebaseUid;
|
||||
modelUser.firebaseUid = Cache().firebaseUid;
|
||||
await CustomerApi().addUser(modelUser);
|
||||
} else {
|
||||
throw new Exception("Facebook signup was not successful. Please try another method");
|
||||
}
|
||||
} on auth.FirebaseAuthException catch (e) {
|
||||
if (e.code == 'email-already-in-use') {
|
||||
log('The account already exists for that email.');
|
||||
throw Exception("The email address has been registered already");
|
||||
}
|
||||
} on WorkoutTestException catch (ex) {
|
||||
if (ex.code == WorkoutTestException.CUSTOMER_EXISTS) {
|
||||
log('The account already exists for that email.');
|
||||
throw Exception("The email address has been registered already");
|
||||
}
|
||||
} on Exception catch (ex) {
|
||||
log("Google exception: " + ex.toString());
|
||||
throw Exception("Google Sign In failed");
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> userData = await FirebaseApi().signInWithFacebook();
|
||||
if (userData != null) {
|
||||
modelUser.email = userData['email'];
|
||||
modelUser.password = "1234567";
|
||||
modelUser.firebaseUid = Cache().facebookUid;
|
||||
await CustomerApi().addUser(modelUser);
|
||||
Future<void> addUserGoogle() async {
|
||||
final User modelUser = this.user;
|
||||
try {
|
||||
Map<String, dynamic> userData = await FirebaseApi().registerWithGoogle();
|
||||
if (userData != null) {
|
||||
modelUser.email = userData['email'];
|
||||
if (modelUser.email == null) {
|
||||
throw new Exception("Google signup was not successful. Please try another method");
|
||||
}
|
||||
modelUser.password = Cache().firebaseUid;
|
||||
modelUser.firebaseUid = Cache().firebaseUid;
|
||||
await CustomerApi().addUser(modelUser);
|
||||
} else {
|
||||
throw new Exception("Google signup was not successful. Please try another method");
|
||||
}
|
||||
} on auth.FirebaseAuthException catch (e) {
|
||||
if (e.code == 'email-already-in-use') {
|
||||
log('The account already exists for that email.');
|
||||
throw Exception("The email address has been registered already");
|
||||
}
|
||||
} on WorkoutTestException catch (ex) {
|
||||
if (ex.code == WorkoutTestException.CUSTOMER_EXISTS) {
|
||||
log('The account already exists for that email.');
|
||||
throw Exception("The email address has been registered already");
|
||||
}
|
||||
} on Exception catch (ex) {
|
||||
log("Google exception: " + ex.toString());
|
||||
throw Exception("Google Sign In failed");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addUserApple() async {
|
||||
final User modelUser = this.user;
|
||||
try {
|
||||
Map<String, dynamic> userData = await FirebaseApi().registerWithApple();
|
||||
if (userData != null) {
|
||||
modelUser.email = userData['email'];
|
||||
if (modelUser.email == null) {
|
||||
throw new Exception("Apple signup was not successful. Please try another method");
|
||||
}
|
||||
modelUser.password = Cache().firebaseUid;
|
||||
modelUser.firebaseUid = Cache().firebaseUid;
|
||||
await CustomerApi().addUser(modelUser);
|
||||
} else {
|
||||
throw new Exception("Apple signup was not successful. Please try another method");
|
||||
}
|
||||
} on auth.FirebaseAuthException catch (e) {
|
||||
if (e.code == 'email-already-in-use') {
|
||||
log('The account already exists for that email.');
|
||||
throw Exception("The email address has been registered already");
|
||||
}
|
||||
} on WorkoutTestException catch (ex) {
|
||||
if (ex.code == WorkoutTestException.CUSTOMER_EXISTS) {
|
||||
log('The account already exists for that email.');
|
||||
throw Exception("The email address has been registered already");
|
||||
}
|
||||
} on Exception catch (ex) {
|
||||
log("Apple exception: " + ex.toString());
|
||||
throw Exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> getUserByFB() async {
|
||||
final User modelUser = this.user;
|
||||
Map<String, dynamic> userData = await FirebaseApi().signInWithFacebook();
|
||||
try {
|
||||
Map<String, dynamic> userData = await FirebaseApi().signInWithFacebook();
|
||||
if (userData == null) {
|
||||
throw new Exception("Facebook login was not successful");
|
||||
}
|
||||
modelUser.email = userData['email'];
|
||||
|
||||
await CustomerApi().getUserByEmail(modelUser.email);
|
||||
await Cache().afterFirebaseLogin();
|
||||
} on FacebookAuthException catch (e) {
|
||||
switch (e.errorCode) {
|
||||
case FacebookAuthErrorCode.OPERATION_IN_PROGRESS:
|
||||
throw Exception("You have a previous Facebook login operation in progress");
|
||||
break;
|
||||
case FacebookAuthErrorCode.CANCELLED:
|
||||
throw Exception("Facebook login cancelled");
|
||||
break;
|
||||
case FacebookAuthErrorCode.FAILED:
|
||||
throw Exception("Facebook login failed");
|
||||
break;
|
||||
}
|
||||
} on NotFoundException catch (ex) {
|
||||
log("FB exception: " + ex.toString());
|
||||
throw Exception("Customer does not exist or the password is wrong");
|
||||
} on Exception catch (e) {
|
||||
log(e.toString());
|
||||
throw new Exception(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> getUserByGoogle() async {
|
||||
final User modelUser = this.user;
|
||||
Map<String, dynamic> userData = await FirebaseApi().signInWithGoogle();
|
||||
if (userData == null || userData['email'] == null) {
|
||||
throw new Exception("Google login was not successful");
|
||||
}
|
||||
modelUser.email = userData['email'];
|
||||
await CustomerApi().getUserByEmail(modelUser.email);
|
||||
await Cache().afterFacebookLogin();
|
||||
try {
|
||||
await CustomerApi().getUserByEmail(modelUser.email);
|
||||
await Cache().afterFirebaseLogin();
|
||||
} on Exception catch (ex) {
|
||||
log("Google exception: " + ex.toString());
|
||||
throw Exception("Customer does not exist or the password is wrong");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> getUserByApple() async {
|
||||
final User modelUser = this.user;
|
||||
Map<String, dynamic> userData = await FirebaseApi().signInWithApple();
|
||||
if (userData == null || userData['email'] == null) {
|
||||
throw new Exception("Apple login was not successful");
|
||||
}
|
||||
modelUser.email = userData['email'];
|
||||
try {
|
||||
await CustomerApi().getUserByEmail(modelUser.email);
|
||||
await Cache().afterFirebaseLogin();
|
||||
} on Exception catch (ex) {
|
||||
log("Apple exception: " + ex.toString());
|
||||
throw Exception("Customer does not exist or the password is wrong");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> getUser() async {
|
||||
@@ -64,7 +205,7 @@ class UserRepository {
|
||||
await CustomerApi().getUserByEmail(modelUser.email);
|
||||
await Cache().afterFirebaseLogin();
|
||||
} else {
|
||||
print("Exception: user not found or password is wrong");
|
||||
log("Exception: user not found or password is wrong");
|
||||
throw Exception("Customer does not exist or the password is wrong");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user