37 lines
1013 B
TypeScript
37 lines
1013 B
TypeScript
import { DEMO_AUTH_EMAIL_COOKIE, DEMO_AUTH_ROLE_COOKIE } from "@/lib/auth/demoAuth";
|
|
|
|
export type ClientAuthSnapshot = {
|
|
userId: string;
|
|
userEmail: string | null;
|
|
isAuthed: boolean;
|
|
isTeacher: boolean;
|
|
};
|
|
|
|
const readCookieMap = (): Map<string, string> =>
|
|
new Map(
|
|
document.cookie
|
|
.split(";")
|
|
.map((entry) => entry.trim())
|
|
.filter(Boolean)
|
|
.map((entry) => {
|
|
const [key, ...rest] = entry.split("=");
|
|
return [key, decodeURIComponent(rest.join("="))] as const;
|
|
}),
|
|
);
|
|
|
|
export const readDemoClientAuth = (): ClientAuthSnapshot => {
|
|
if (typeof document === "undefined") {
|
|
return { userId: "guest", userEmail: null, isAuthed: false, isTeacher: false };
|
|
}
|
|
|
|
const cookies = readCookieMap();
|
|
const userEmail = cookies.get(DEMO_AUTH_EMAIL_COOKIE) ?? null;
|
|
const role = cookies.get(DEMO_AUTH_ROLE_COOKIE) ?? "";
|
|
return {
|
|
userId: userEmail ?? "guest",
|
|
userEmail,
|
|
isAuthed: Boolean(userEmail),
|
|
isTeacher: role === "teacher",
|
|
};
|
|
};
|