registerUser method

Future<String> registerUser({
  1. required String email,
  2. required String password,
  3. Map<String, dynamic> userData = const {},
  4. String? customUserID,
})

this will return a jwt for the user to use to sign in again without the need of the email and password again

Implementation

Future<String> registerUser({
  required String email,
  required String password,
  Map<String, dynamic> userData = const {},
  String? customUserID,
}) async {
  //! i want to add the functionality of reverting everything if an error occurred
  String id = customUserID ?? ObjectId().toHexString();

  String passwordHash = SecurePassword(password).getPasswordHash();

  // first check if email already exists
  AuthModel? savedModel = await authDbProvider.getUserByEmail(email);
  if (savedModel != null) {
    throw DuplicateEmailException();
  }
  AuthModel authModel = AuthModel(
    id: id,
    email: email,
    passwordHash: passwordHash,
  );
  // creating the jwt
  bool authModelSaved = await authDbProvider.saveUserAuth(authModel);

  if (!authModelSaved) {
    throw DBWriteException('failed to register user');
  }
  // saving user data
  userData[DBRKeys.id] = authModel.id;
  var userDataSaved = await authDbProvider.saveUserData(userData);
  if (!userDataSaved) {
    throw DBWriteException('failed to save user data');
  }

  String jwtToken =
      await authDbProvider.createJwtAndSave(authModel.id, email);

  return jwtToken;
}