14 lines
734 B
TypeScript
14 lines
734 B
TypeScript
// Change the password. Signs out every other device.
|
|
export default defineEventHandler(async event => {
|
|
rateLimit(event, 'login', 20, 10 * 60_000)
|
|
const user = await requireUser(event)
|
|
const body = await readBody<{ current?: unknown; next?: unknown }>(event)
|
|
if (typeof body?.current !== 'string' || !(await verifyPassword(body.current, user))) {
|
|
throw createError({ statusCode: 403, message: 'The current password is not right' })
|
|
}
|
|
if (!validPassword(body.next)) throw createError({ statusCode: 400, message: 'The new password needs at least 8 characters' })
|
|
await users().setItem(`user:${user.id}`, { ...user, ...(await hashPassword(body.next)) })
|
|
await endAllSessions(user.id, event)
|
|
return { ok: true }
|
|
})
|