aboutsummaryrefslogtreecommitdiffstats
path: root/helpers/PasswordHash.ts
blob: 300186d8345cdd9a0c32efdf2f5c66bcce4b1e87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import User from '#app/Models/User';
import hash from '@adonisjs/core/services/hash';

export async function handleVerifyAndReHash(
  user: User,
  passwordToTest: string,
): Promise<boolean> {
  // Verify password
  const usesLegacyHasher = /^\$2[aby]/.test(user.password);
  let isMatchedPassword = false;

  isMatchedPassword = await (usesLegacyHasher
    ? hash.use('legacy').verify(user.password, passwordToTest)
    : hash.verify(user.password, passwordToTest));

  // TODO: For some reason this is not working (user can't login after re-hashing)
  // rehash user password
  // if (usesLegacyHasher && isMatchedPassword) {
  //   user.password = await Hash.make(passwordToTest);
  //   await user.save();
  // }

  return isMatchedPassword;
}