21 lines
569 B
TypeScript
21 lines
569 B
TypeScript
import { z } from 'zod'
|
|
|
|
const FAILED_LOGIN_DELAY_MS = 1000
|
|
|
|
const loginSchema = z.object({
|
|
password: z.string().min(1)
|
|
})
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const { password } = await readValidBody(event, loginSchema)
|
|
|
|
if (!(await passwordMatches(event, password))) {
|
|
await new Promise(resolve => setTimeout(resolve, FAILED_LOGIN_DELAY_MS))
|
|
throw createError({ statusCode: 401, statusMessage: 'Wrong password' })
|
|
}
|
|
|
|
const session = await authSession(event)
|
|
await session.update({ unlocked: true })
|
|
return { unlocked: true }
|
|
})
|