👔 (necktie) add logic to store the cookies

This commit is contained in:
2025-10-12 00:12:42 +07:00
parent 94f6f0780c
commit 273db57e7b
4 changed files with 29 additions and 3 deletions

View File

@ -4,7 +4,10 @@ import { apiErrorHandler } from "@/shared/lib/ky/errorHandler";
import { RegisterInputs } from "../ui/components/ProvisionInput";
import { ServerRequestCallback } from "@/shared/types/ServerRequestCallback";
import { generateRandomString } from "@/shared/helper/generateRandomString";
import { CallbackFromBackend } from "../types/callbackFromBackend";
import { cookies } from "next/headers";
import { api } from "@/shared/lib/ky/connector";
import { COOKIE_KEYS } from "@/shared/constants/cookie.key";
export const submitRegisterForm = async (
data: RegisterInputs
@ -30,13 +33,25 @@ export const submitRegisterForm = async (
email: data.email,
password: data.password,
};
const callback = await api.post("users", { json: payload }).json();
const callback = (await api
.post("users", { json: payload })
.json()) as CallbackFromBackend<string>;
(await cookies()).set({
name: COOKIE_KEYS["AUTH"],
value: callback.data!,
httpOnly: true,
secure: process.env.NODE_ENV === "production",
path: "/",
sameSite: "lax",
maxAge: 60 * 60 * 24 * 7,
});
return {
success: true,
status: 200,
text: { message: "Registration successful" },
data: callback,
data: callback.data,
};
} catch (error) {
return apiErrorHandler(error);

View File

@ -0,0 +1,6 @@
export interface CallbackFromBackend<T> {
success: boolean;
status: number;
message: string;
data?: T;
}

View File

@ -6,6 +6,8 @@ import { SubmitHandler, useForm } from "react-hook-form";
import { submitRegisterForm } from "../../lib/submitRegisterForm";
import { zodResolver } from "@hookform/resolvers/zod";
import { registerFormSchema } from "../../models/registerForm.schema";
import { COOKIE_KEYS } from "@/shared/constants/cookie.key";
import { useRunOnce } from "@/shared/hooks/useRunOnce";
type Props = {
fullname: string;
@ -43,7 +45,6 @@ const ProvisionInput = ({ fullname }: Props) => {
description: returnData.text.message,
});
} else {
console.log(returnData);
setSubmitStatus(false);
addToast({
color: "success",

View File

@ -0,0 +1,4 @@
export const COOKIE_KEYS = {
AUTH: "auth_token",
CSRF: "csrf_token",
};