Skip to content

Commit

Permalink
feat: add verifyPassword method to user for verifying password agains…
Browse files Browse the repository at this point in the history
…t hash
  • Loading branch information
thetutlage committed Dec 16, 2024
1 parent 3e9be70 commit 5dbe70f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/mixins/lucid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,26 @@ export function withAuthFinder(
throw new E_INVALID_CREDENTIALS('Invalid user credentials')
}

const passwordHash = (user as any)[options.passwordColumnName]
if (!passwordHash) {
throw new RuntimeException(
`Cannot verify password during login. The value of column "${options.passwordColumnName}" is undefined or null`
)
}

if (await hash().verify(passwordHash, password)) {
if (await user.verifyPassword(password)) {
return user
}

throw new E_INVALID_CREDENTIALS('Invalid user credentials')
}

/**
* Verifies the plain password against the user's password
* hash
*/
verifyPassword(plainPassword: string): Promise<boolean> {
const passwordHash = (this as any)[options.passwordColumnName]
if (!passwordHash) {
throw new RuntimeException(
`Cannot verify password. The value for "${options.passwordColumnName}" column is undefined or null`
)
}
return hash().verify(passwordHash, plainPassword)
}
}

return UserWithUserFinder
Expand Down
2 changes: 1 addition & 1 deletion tests/auth/mixins/with_auth_finder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ test.group('withAuthFinder | verify', () => {

await assert.rejects(
() => User.verifyCredentials('[email protected]', 'supersecret'),
'Cannot verify password during login. The value of column "password" is undefined or null'
'Cannot verify password. The value for "password" column is undefined or null'
)
})

Expand Down

0 comments on commit 5dbe70f

Please sign in to comment.