resetPassword static method

Future<bool> resetPassword(
  1. Session session,
  2. String verificationCode,
  3. String password
)

Resets a users password using a password reset verification code.

Implementation

static Future<bool> resetPassword(
  Session session,
  String verificationCode,
  String password,
) async {
  var passwordResets = await EmailReset.db.deleteWhere(
    session,
    where: (t) => t.verificationCode.equals(verificationCode),
  );

  if (passwordResets.isEmpty) {
    session.log(
      'Verification code is invalid!',
      level: LogLevel.debug,
    );
    return false;
  }

  var passwordReset = passwordResets.first;

  if (passwordReset.expiration.isBefore(DateTime.now().toUtc())) {
    session.log(
      'Verification code has expired!',
      level: LogLevel.debug,
    );
    return false;
  }

  var emailAuth = await EmailAuth.db.findFirstRow(session, where: (t) {
    return t.userId.equals(passwordReset.userId);
  });

  if (emailAuth == null) {
    session.log(
      "User with id: '${passwordReset.userId}' has no email authentication!",
      level: LogLevel.debug,
    );
    return false;
  }

  emailAuth.hash = await generatePasswordHash(password);
  await EmailAuth.db.updateRow(session, emailAuth);

  return true;
}