changePassword static method

Future<bool> changePassword(
  1. Session session,
  2. int userId,
  3. String oldPassword,
  4. String newPassword,
)

Updates the password of a user.

Implementation

static Future<bool> changePassword(
  Session session,
  int userId,
  String oldPassword,
  String newPassword,
) async {
  var auth = await session.db.findSingleRow<EmailAuth>(
    where: EmailAuth.t.userId.equals(userId),
  );
  if (auth == null) {
    return false;
  }

  // Check old password
  if (auth.hash != generatePasswordHash(oldPassword, auth.email)) {
    return false;
  }

  // Update password
  auth.hash = generatePasswordHash(newPassword, auth.email);
  await session.db.update(auth);

  return true;
}