17 lines
619 B
TypeScript
17 lines
619 B
TypeScript
import { compare } from 'bcryptjs'
|
|
import { readBody, createError } from 'h3'
|
|
import { prisma } from '../../utils/db'
|
|
import { setSession } from '../../utils/auth'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody<{ email: string; password: string }>(event)
|
|
const user = await prisma.user.findUnique({ where: { email: body.email } })
|
|
|
|
if (!user || !(await compare(body.password, user.passwordHash))) {
|
|
throw createError({ statusCode: 401, statusMessage: 'Invalid credentials' })
|
|
}
|
|
|
|
setSession(event, user.id)
|
|
return { id: user.id, email: user.email, name: user.name }
|
|
})
|