news-analyze/server/api/user/checkcookie.ts
吳元皓 81012f5061 Made the line_today.py kinda work ig. But I have no idea how can I run
this without issues in prod tho. and the "BlurPageBeforeLogin" thing
works just file, oh and checkCookie is now working (but without the
database part just yet)
2025-05-17 23:31:55 +08:00

48 lines
1.2 KiB
TypeScript

// This should be hooked up to a database soon.
import postgres from "~/server/components/postgres";
// Parse Date Function
function checkDate(dateString: string) {
const now = new Date();
const parsed = new Date(dateString);
const timer = 60 * 60 * 1;
return now.getTime() - parsed.getTime() > timer;
}
export default defineEventHandler(async (event) => {
const loginCookie = getCookie(event, "session");
const lastCheckCookie = getCookie(event, "last_check");
const nowDate = new Date().toLocaleString();
console.log(nowDate);
if (!lastCheckCookie && loginCookie) {
deleteCookie(event, "session");
setCookie(event, "lastCheckCookie", nowDate, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
path: "/",
});
return {
auth: false,
user: null,
};
}
if (!lastCheckCookie) {
setCookie(event, "lastCheckCookie", nowDate, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
path: "/",
});
}
if (checkDate(String(lastCheckCookie))) {
setCookie(event, "lastCheckCookie", nowDate, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
path: "/",
});
}
return {
auth: true,
user: "testing",
};
});