summaryrefslogtreecommitdiff
path: root/src/actions/login.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/actions/login.ts')
-rw-r--r--src/actions/login.ts31
1 files changed, 23 insertions, 8 deletions
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) {