registerUserWithEmailAndPassword method
Implementation
@override
Future<Either<AuthServiceSignInFailure, Unit>> registerUserWithEmailAndPassword(
String email, String password) async {
try {
// Call the function to create the auth user and db user
var result = await authCallable.call({
'action': 'authRegisterWithEmailAndPassword',
'email': email,
'password': password,
});
logd('Register result: ${result.data}');
// // Create the auth user
// fb_auth.UserCredential userCred =
// await _auth.createUserWithEmailAndPassword(email: email, password: password);
// if (userCred.user == null) {
// return left(AuthServiceSignInFailure.unexpected);
// }
// // Create, Get the firebase user and return
// var dbCreateResult = await Get.find<UserRepositoryInt>().createUserIfNotExist(User(
// id: userCred.user!.uid,
// email: email,
// ));
// // Determine outcome
// Either<AuthServiceSignInFailure, User> returnVal = dbCreateResult.fold(
// (failure) {
// return failure.maybeWhen(
// orElse: () => left(AuthServiceSignInFailure.databaseError()),
// );
// },
// (success) {
// currentUser.value = success;
// return right(success);
// },
// );
// // Finally return
// return returnVal;
// Sometimes we can't sign in right away after creating the account
const int maxRetries = 5;
int i = 0;
for (i; i < maxRetries; i++) {
try {
await _fbAuth.signInWithEmailAndPassword(email: email, password: password);
} on fb_auth.FirebaseAuthException catch (f) {
if (f.message == 'user-not-found') {
// Delay
logd('AUTH SIGN IN FAILED. Waiting a second then trying again');
await Future.delayed(const Duration(seconds: 1));
continue;
} else {
rethrow;
}
}
break;
}
if (i >= maxRetries) {
//TODO: a better error?
return left(AuthServiceSignInFailure.unexpected);
}
//TODO: is this redundant? Because I'm not sure it gets it in tiem without it
//await refreshCurrentUser();
return right(unit);
// } on fb_auth.FirebaseAuthException catch (e) {
// switch (e.code) {
// case 'invalid-email':
// logd('registerUserWithEmailAndPassword invalid-email');
// return left(AuthServiceSignInFailure.invalidEmail);
// case 'email-already-in-use':
// logd('registerUserWithEmailAndPassword email-already-in-use');
// return left(AuthServiceSignInFailure.userAlreadyExists);
// case 'weak-password':
// logd('registerUserWithEmailAndPassword weak-password');
// return left(AuthServiceSignInFailure.weakPassword);
// default:
// logd('registerUserWithEmailAndPassword default');
// return left(AuthServiceSignInFailure.unexpected);
// }
} on FirebaseFunctionsException catch (e) {
logd('registerUserWithEmailAndPassword FirebaseFunctionsException: ${e.message}');
switch (e.message) {
case 'email-already-in-use':
return left(AuthServiceSignInFailure.userAlreadyExists);
case 'weak-password':
return left(AuthServiceSignInFailure.weakPassword);
case 'invalid-email':
return left(AuthServiceSignInFailure.invalidEmail);
default:
return left(AuthServiceSignInFailure.unexpected);
}
} catch (e) {
logd('registerUserWithEmailAndPassword other exception');
loge(e);
return left(AuthServiceSignInFailure.unexpected);
}
}