CoastalCommitsPastes/client/pages/api/auth/signin.ts

34 lines
804 B
TypeScript
Raw Normal View History

2022-11-09 21:38:05 -05:00
import { NextApiRequest, NextApiResponse } from "next"
import prisma from "app/prisma"
import bcrypt from "bcrypt"
import { signin } from "@lib/server/signin"
2022-11-09 21:38:05 -05:00
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { username, password } = req.body
if (!username || !password) {
return res.status(400).json({ error: "Missing param" })
}
const user = await prisma.user.findFirst({
where: {
username
}
})
if (!user) {
return res.status(401).json({ error: "Unauthorized" })
}
const isPasswordValid = await bcrypt.compare(password, user.password)
if (!isPasswordValid) {
return res.status(401).json({ error: "Unauthorized" })
}
2022-11-09 22:02:06 -05:00
const token = await signin(user.id, req, res);
2022-11-09 21:38:05 -05:00
return res.status(201).json({ token: token, userId: user.id })
}