From 490388360a0852bcf8ee054e96fa90e166df5792 Mon Sep 17 00:00:00 2001 From: polwex Date: Thu, 29 May 2025 14:52:38 +0700 Subject: fucker actually solved the cookies, love ya man --- src/actions/login.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'src/actions/login.ts') diff --git a/src/actions/login.ts b/src/actions/login.ts index ee15fe6..ed96f54 100644 --- a/src/actions/login.ts +++ b/src/actions/login.ts @@ -50,20 +50,35 @@ export async function postLogin( console.log({ res }); if ("error" in res) return { error: res.error }; else { - setCookie(res.ok as number); - return { success: true }; + // Set the cookie + await setCookie(res.ok as number); + + // Return success for client-side handling + return { + success: true, + userId: res.ok, + redirect: "/" + }; } } async function setCookie(userId: number) { + // Set cookie expiry for 30 days const COOKIE_EXPIRY = Date.now() + 1000 * 60 * 60 * 24 * 30; - const COOKIE_OPTS = { expires: new Date(COOKIE_EXPIRY) }; - + + // Generate a secure random token for the cookie const { randomBytes } = await import("node:crypto"); - const cokistring = randomBytes(32).toBase64(); - const res = db.setCookie(cokistring, userId, COOKIE_EXPIRY); + const cookieToken = randomBytes(32).toString("base64"); + + // Store the cookie in the database + const res = db.setCookie(cookieToken, userId, COOKIE_EXPIRY); + + // Set the cookie in the response const { setCookie } = useCookies(); - setCookie(cokistring); - // unstable_redirect("/"); + setCookie(cookieToken); + + console.log("Cookie set for user ID:", userId); + + // Redirect is managed by client after successful login } // export async function postLogout(prev: number) { -- cgit v1.2.3