verifyNumericCode static method

Future<bool> verifyNumericCode(
  1. String email,
  2. String code
)

Verify numeric verification code

Implementation

static Future<bool> verifyNumericCode(String email, String code) async {
  await Auth.ensureFrameworkTablesExist();

  // Find matching valid record
  final record = await QueryBuilder(table: 'email_verification_tokens')
      .where('email', '=', email)
      .where('expires_at', '>', DateTime.now().toIso8601String())
      .first();

  if (record == null) {
    Log.warning('โŒ Invalid or expired verification code for $email');
    return false;
  }

  final isValid = Hashing().verify(code, record['token']);

  if (!isValid) {
    Log.warning('โŒ Invalid verification code for $email');
    return false;
  }
  // Mark email as verified
  final emailVerifiedAtExists =
      await Auth.columnExists(Auth.config.table, 'email_verified_at');
  if (emailVerifiedAtExists) {
    await QueryBuilder(table: Auth.config.table)
        .where(Auth.config.emailColumn, '=', email)
        .update({
      'email_verified_at': DateTime.now().toIso8601String(),
    });
  }

  // ๐Ÿงน Delete used code
  await QueryBuilder(table: 'email_verification_tokens')
      .where('email', '=', email)
      .delete();

  Log.debug('โœ… Email verified successfully: $email');
  return true;
}