add step in auth verification

This commit is contained in:
rafiarrafif
2025-05-25 14:10:25 +07:00
parent d0e4e1e835
commit 03fd0531af
15 changed files with 237 additions and 23 deletions

View File

@ -0,0 +1,33 @@
import { serialize } from "cookie";
export const clearCookies = (
set: any,
cookieKeys: string[],
options?: Partial<{
httpOnly: boolean;
secure: boolean;
sameSite: "strict" | "lax" | "none";
path: string;
}>
) => {
// Define the default configurations for clearing cookies
const defaultOptions = {
httpOnly: true,
secure: true,
sameSite: "strict" as const,
path: "/",
...options,
};
// Create an array of cleared cookies with the specified names
const clearedCookies = cookieKeys.map((name) => {
return serialize(name, "", {
...defaultOptions,
expires: new Date(0),
});
});
// Set the cleared cookies in the response headers
set.headers["set-cookie"] = clearedCookies;
return clearedCookies;
};

View File

@ -1,15 +1,34 @@
import { serialize } from "cookie";
export const setCookie = async (set: any, payload: string) => {
export const setCookie = async (
set: any,
name: string,
payload: string,
options?: Partial<{
httpOnly: boolean;
secure: boolean;
sameSite: "strict" | "lax" | "none";
maxAge: number;
path: string;
}>
) => {
// Define the default configurations for the cookie
const cookieLifetime = Number(process.env.SESSION_EXPIRE!);
const serializedCookie = serialize("auth_token", payload, {
const defaultOptions = {
httpOnly: true,
secure: true,
sameSite: "strict",
sameSite: "strict" as const,
maxAge: cookieLifetime,
path: "/",
});
};
// Merge the default options with the provided options
const finalOptions = { ...defaultOptions, ...options };
// Create the serialized cookie string
const serializedCookie = serialize(name, payload, finalOptions);
// Set the cookie in the response headers
set.headers["set-cookie"] = serializedCookie;
return serializedCookie;
};