registerWithEmail method

Future<User?> registerWithEmail(
  1. String email,
  2. String password
)

Registers a new user with email and password.

  • email: The user's email address.
  • password: The user's password.

Returns the User object on success, or null on failure.

Implementation

Future<User?> registerWithEmail(String email, String password) async {
  try {
    UserCredential userCredential = await auth.createUserWithEmailAndPassword(
      email: email,
      password: password,
    );
    return userCredential.user;
  } on FirebaseAuthException catch (e) {
    // Handle registration-specific errors
    switch (e.code) {
      case 'email-already-in-use':
        print('The email address is already in use.');
        break;
      case 'weak-password':
        print('The provided password is too weak.');
        break;
      default:
        print('Error registering with email: ${e.message}');
    }
    return null;
  } catch (e) {
    print('Unexpected error registering with email: $e');
    return null;
  }
}