Added cookie set on login

This commit is contained in:
sianida26 2024-03-17 10:33:40 +07:00
parent 245927d75b
commit ad8f465a4f

View File

@ -3,29 +3,30 @@ import handleCatchApi from "@/core/utils/handleCatchApi";
import AuthError from "@/modules/auth/error/AuthError"; import AuthError from "@/modules/auth/error/AuthError";
import signInSchema from "@/modules/auth/formSchemas/signInSchema"; import signInSchema from "@/modules/auth/formSchemas/signInSchema";
import signIn from "@/modules/auth/services/signIn"; import signIn from "@/modules/auth/services/signIn";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
export const dynamic = "force-dynamic"; export const dynamic = "force-dynamic";
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
try { try {
applicationJsonOnly(request.headers) applicationJsonOnly(request.headers);
const data = signInSchema.safeParse(await request.json()); const data = signInSchema.safeParse(await request.json());
if (!data.success){ if (!data.success) {
throw new AuthError({ throw new AuthError({
errorCode: "INVALID_CREDENTIALS", errorCode: "INVALID_CREDENTIALS",
message: "Email or Password does not match", message: "Email or Password does not match",
statusCode: 401 statusCode: 401,
}) });
} }
const result = await signIn(data.data) const result = await signIn(data.data);
request.cookies.set("token", result.token) cookies().set("token", result.token);
return NextResponse.json(result); return NextResponse.json(result);
} catch (e) { } catch (e) {
return handleCatchApi(e) return handleCatchApi(e);
} }
} }