👔 (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 { 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);
|
||||
|
||||
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 { 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",
|
||||
|
||||
Reference in New Issue
Block a user