2026-10-06 02:02:08 +00:00
|
|
|
import "server-only";
|
|
|
|
|
import { cache } from "react";
|
|
|
|
|
import { cookies } from "next/headers";
|
|
|
|
|
import { redirect } from "next/navigation";
|
2026-10-21 04:39:15 +00:00
|
|
|
import { clockAuthDestination } from "./clock-pwa";
|
2026-10-06 02:02:08 +00:00
|
|
|
import { sessionFromToken } from "./auth-service";
|
|
|
|
|
import { SESSION_COOKIE, authDestination } from "./auth-policy";
|
|
|
|
|
|
|
|
|
|
// React cache scopes this result to one render, never across users or requests.
|
|
|
|
|
export const getSession = cache(async () => {
|
|
|
|
|
const token = (await cookies()).get(SESSION_COOKIE)?.value;
|
|
|
|
|
return token ? sessionFromToken(token) : null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export async function requireUser() {
|
|
|
|
|
const session = await getSession();
|
|
|
|
|
const destination = authDestination(session);
|
|
|
|
|
if (destination !== "/") redirect(destination);
|
|
|
|
|
return session!;
|
|
|
|
|
}
|
2026-10-21 04:39:15 +00:00
|
|
|
|
|
|
|
|
export async function requireClockUser() {
|
|
|
|
|
const session=await getSession();
|
|
|
|
|
const destination=clockAuthDestination(session);
|
|
|
|
|
if(destination!=="/clock")redirect(destination);
|
|
|
|
|
return session!;
|
|
|
|
|
}
|