signUpWithEmailAndPassword method
//////////// //////////// Signs up a new user with email and password
Implementation
// II.B - Email & Password Authentication
///////////////
/// Signs up a new user with email and password
Future<UserCredential> signUpWithEmailAndPassword(
String email,
String password,
String displayName
) async {
try {
// Pre-condition checks
if (email.isEmpty || password.isEmpty || displayName.isEmpty) {
throw ValidationError('Email, password, and display name are required');
}
// Create the user
final userCredential = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
// Update the user's display name
if (userCredential.user != null) {
await userCredential.user?.updateDisplayName(displayName);
} else {
throw StateError('User was created but not returned by Firebase');
}
return userCredential;
} on FirebaseAuthException catch (e) {
// Handle specific Firebase Auth errors
switch (e.code) {
case 'email-already-in-use':
throw ValidationError('An account already exists with this email', cause: e);
case 'invalid-email':
throw ValidationError('The email address is not valid', cause: e);
case 'operation-not-allowed':
throw StateError('Email/password accounts are not enabled', cause: e);
case 'weak-password':
throw ValidationError('The password is too weak', cause: e);
default:
throw RALIException('Failed to create account: ${e.message}', cause: e);
}
} catch (e) {
// Handle other error types
if (e is RALIException) {
// Already a RALIException, just rethrow
rethrow;
}
// Log the error for debugging
logError(e, StackTrace.current, context: 'signUpWithEmailAndPassword');
throw RALIException('An unexpected error occurred during sign up', cause: e);
}
}