2024-06-26 20:38:34 -05:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import verifyByCredentials from "@/lib/api/verifyByCredentials";
|
|
|
|
import createSession from "@/lib/api/controllers/session/createSession";
|
2024-09-14 15:00:19 -05:00
|
|
|
import { PostSessionSchema } from "@/lib/shared/schemaValidation";
|
2024-06-26 20:38:34 -05:00
|
|
|
|
|
|
|
export default async function session(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse
|
|
|
|
) {
|
2024-09-14 15:00:19 -05:00
|
|
|
const dataValidation = PostSessionSchema.safeParse(req.body);
|
|
|
|
|
|
|
|
if (!dataValidation.success) {
|
|
|
|
return res.status(400).json({
|
|
|
|
response: `Error: ${
|
|
|
|
dataValidation.error.issues[0].message
|
|
|
|
} [${dataValidation.error.issues[0].path.join(", ")}]`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const { username, password, sessionName } = dataValidation.data;
|
2024-06-26 20:38:34 -05:00
|
|
|
|
|
|
|
const user = await verifyByCredentials({ username, password });
|
|
|
|
|
|
|
|
if (!user)
|
|
|
|
return res.status(400).json({
|
|
|
|
response:
|
|
|
|
"Invalid credentials. You might need to reset your password if you're sure you already signed up with the current username/email.",
|
|
|
|
});
|
|
|
|
|
|
|
|
if (req.method === "POST") {
|
|
|
|
const token = await createSession(user.id, sessionName);
|
|
|
|
return res.status(token.status).json({ response: token.response });
|
|
|
|
}
|
|
|
|
}
|