next-enterprise/pages/api/login.js

36 lines
1 KiB
JavaScript
Raw Normal View History

2024-02-29 09:33:34 +01:00
import { createTokens, hashPassword, verifyPassword } from "../../utils/auth"
export default async function handler(req, res) {
if (req.method !== "POST") {
return res.status(405).json({ message: "Method Not Allowed" })
}
const { email, password } = req.body
console.log(password)
// You'd need to fetch user details from your database or any data source
//fetch data from postgres database where we store password as hash
// Here, for demonstration, let's assume a hardcoded user
2024-03-13 20:16:29 +01:00
const response = await fetch(`http://localhost:3001/user`)
const user_data = await response.json()
2024-02-29 09:33:34 +01:00
2024-03-13 20:16:29 +01:00
const isValid = await verifyPassword(password, user_data.password)
2024-02-29 09:33:34 +01:00
if (!isValid) {
return res.status(401).json({ message: "Invalid credentials" })
}
2024-03-13 20:16:29 +01:00
const token = await createTokens(user_data.id)
2024-02-29 09:33:34 +01:00
res.setHeader("Set-Cookie", [
`token=${token.accessToken}; Path=/; HttpOnly`,
`rtoken=${token.refreshToken}; Path=/; HttpOnly`,
])
return res.status(200).json({ token })
}
//user@example.com
//password123