aboutsummaryrefslogtreecommitdiffstats
path: root/helpers/PasswordHash.ts
blob: be3da2ce9e69da478349af1827f4f363b4fc5cea (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 '@ioc:Adonis/Core/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;
}