Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Savy011/acm-website-tanstack.
Something went wrong. Try again.
729 B · 24 lines
TypeScript
at main
12345678910111213141516171819202122232425import type { SessionUser } from "$lib/api/types";
import { getCookie } from "@tanstack/react-start/server";
import { decodeJWTPayload } from "$lib/utils";
type Session = { authenticated: true; user: SessionUser; token: string } | { authenticated: false };
export function readSession(): Session { const token = getCookie("session"); const userRaw = getCookie("user");
if (!token || !userRaw) return { authenticated: false };
const claims = decodeJWTPayload(token); if (!claims || Date.now() >= claims.exp * 1000) return { authenticated: false };
try { const user = JSON.parse(userRaw) as SessionUser; return { authenticated: true, user, token }; } catch { return { authenticated: false }; }}