👔 (necktie) add logic to store the cookies
This commit is contained in:
@ -4,7 +4,10 @@ import { apiErrorHandler } from "@/shared/lib/ky/errorHandler";
|
|||||||
import { RegisterInputs } from "../ui/components/ProvisionInput";
|
import { RegisterInputs } from "../ui/components/ProvisionInput";
|
||||||
import { ServerRequestCallback } from "@/shared/types/ServerRequestCallback";
|
import { ServerRequestCallback } from "@/shared/types/ServerRequestCallback";
|
||||||
import { generateRandomString } from "@/shared/helper/generateRandomString";
|
import { generateRandomString } from "@/shared/helper/generateRandomString";
|
||||||
|
import { CallbackFromBackend } from "../types/callbackFromBackend";
|
||||||
|
import { cookies } from "next/headers";
|
||||||
import { api } from "@/shared/lib/ky/connector";
|
import { api } from "@/shared/lib/ky/connector";
|
||||||
|
import { COOKIE_KEYS } from "@/shared/constants/cookie.key";
|
||||||
|
|
||||||
export const submitRegisterForm = async (
|
export const submitRegisterForm = async (
|
||||||
data: RegisterInputs
|
data: RegisterInputs
|
||||||
@ -30,13 +33,25 @@ export const submitRegisterForm = async (
|
|||||||
email: data.email,
|
email: data.email,
|
||||||
password: data.password,
|
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 {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
status: 200,
|
status: 200,
|
||||||
text: { message: "Registration successful" },
|
text: { message: "Registration successful" },
|
||||||
data: callback,
|
data: callback.data,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return apiErrorHandler(error);
|
return apiErrorHandler(error);
|
||||||
|
|||||||
6
features/auth/types/callbackFromBackend.ts
Normal file
6
features/auth/types/callbackFromBackend.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export interface CallbackFromBackend<T> {
|
||||||
|
success: boolean;
|
||||||
|
status: number;
|
||||||
|
message: string;
|
||||||
|
data?: T;
|
||||||
|
}
|
||||||
@ -6,6 +6,8 @@ import { SubmitHandler, useForm } from "react-hook-form";
|
|||||||
import { submitRegisterForm } from "../../lib/submitRegisterForm";
|
import { submitRegisterForm } from "../../lib/submitRegisterForm";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { registerFormSchema } from "../../models/registerForm.schema";
|
import { registerFormSchema } from "../../models/registerForm.schema";
|
||||||
|
import { COOKIE_KEYS } from "@/shared/constants/cookie.key";
|
||||||
|
import { useRunOnce } from "@/shared/hooks/useRunOnce";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
fullname: string;
|
fullname: string;
|
||||||
@ -43,7 +45,6 @@ const ProvisionInput = ({ fullname }: Props) => {
|
|||||||
description: returnData.text.message,
|
description: returnData.text.message,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
console.log(returnData);
|
|
||||||
setSubmitStatus(false);
|
setSubmitStatus(false);
|
||||||
addToast({
|
addToast({
|
||||||
color: "success",
|
color: "success",
|
||||||
|
|||||||
4
shared/constants/cookie.key.ts
Normal file
4
shared/constants/cookie.key.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export const COOKIE_KEYS = {
|
||||||
|
AUTH: "auth_token",
|
||||||
|
CSRF: "csrf_token",
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user