-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathupdate-user-password.ts
More file actions
35 lines (28 loc) · 1010 Bytes
/
update-user-password.ts
File metadata and controls
35 lines (28 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { InvalidTokenError } from '@/domain/errors/invalid-token-error'
import {
invalidateMagicToken,
selectMagicToken,
} from '@/infra/db/repositories/magic-tokens'
import { updateUserPassword } from '@/infra/db/repositories/user-repository'
import type { updateUserPasswordBodySchema } from '@/infra/http/schemas/users'
import { hashPassword } from '@/utils/password'
type UpdatePasswordInput = typeof updateUserPasswordBodySchema._type
export async function updatePasswordService({
password,
token,
}: UpdatePasswordInput) {
const [tokenRecord] = await selectMagicToken(token)
if (!tokenRecord) {
return new InvalidTokenError()
}
if (tokenRecord.used) {
return new InvalidTokenError()
}
if (tokenRecord.expiresAt < new Date()) {
return new InvalidTokenError()
}
const hashedPassword = await hashPassword(password)
await updateUserPassword(tokenRecord.userId, hashedPassword)
await invalidateMagicToken(tokenRecord.token)
return { status: 'password_set' }
}