43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import type { AuthenticatedUser } from "@/types";
|
|
|
|
const TOKEN_STORAGE_KEY = "live-recorder-token";
|
|
const USER_STORAGE_KEY = "live-recorder-user";
|
|
const PREVIEW_TOKEN = "dev-preview-token";
|
|
|
|
const previewUser: AuthenticatedUser = {
|
|
userId: "dev-preview",
|
|
username: "preview",
|
|
displayName: "UI Preview",
|
|
token: PREVIEW_TOKEN,
|
|
expiresAt: "2099-12-31T23:59:59.000Z"
|
|
};
|
|
|
|
export const isNoBackendPreviewMode = import.meta.env.DEV && import.meta.env.VITE_PREVIEW_NO_BACKEND === "1";
|
|
|
|
function hasUsablePreviewUser(value: string | null) {
|
|
if (!value) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(value) as Partial<AuthenticatedUser>;
|
|
return Boolean(parsed.userId && parsed.username && parsed.displayName);
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function ensurePreviewSession() {
|
|
if (!isNoBackendPreviewMode || typeof window === "undefined") {
|
|
return;
|
|
}
|
|
|
|
if (!localStorage.getItem(TOKEN_STORAGE_KEY)) {
|
|
localStorage.setItem(TOKEN_STORAGE_KEY, PREVIEW_TOKEN);
|
|
}
|
|
|
|
if (!hasUsablePreviewUser(localStorage.getItem(USER_STORAGE_KEY))) {
|
|
localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(previewUser));
|
|
}
|
|
}
|