diff options
Diffstat (limited to 'helpers/PasswordHash.ts')
-rw-r--r-- | helpers/PasswordHash.ts | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/helpers/PasswordHash.ts b/helpers/PasswordHash.ts new file mode 100644 index 0000000..be3da2c --- /dev/null +++ b/helpers/PasswordHash.ts | |||
@@ -0,0 +1,24 @@ | |||
1 | import User from 'App/Models/User'; | ||
2 | import Hash from '@ioc:Adonis/Core/Hash'; | ||
3 | |||
4 | export async function handleVerifyAndReHash( | ||
5 | user: User, | ||
6 | passwordToTest: string, | ||
7 | ): Promise<boolean> { | ||
8 | // Verify password | ||
9 | const usesLegacyHasher = /^\$2[aby]/.test(user.password); | ||
10 | let isMatchedPassword = false; | ||
11 | |||
12 | isMatchedPassword = await (usesLegacyHasher | ||
13 | ? Hash.use('legacy').verify(user.password, passwordToTest) | ||
14 | : Hash.verify(user.password, passwordToTest)); | ||
15 | |||
16 | // TODO: For some reason this is not working (user can't login after re-hashing) | ||
17 | // rehash user password | ||
18 | // if (usesLegacyHasher && isMatchedPassword) { | ||
19 | // user.password = await Hash.make(passwordToTest); | ||
20 | // await user.save(); | ||
21 | // } | ||
22 | |||
23 | return isMatchedPassword; | ||
24 | } | ||