advance
This commit is contained in:
36
lib/auth/clientAuth.ts
Normal file
36
lib/auth/clientAuth.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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",
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user