demo-session.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use client";
  2. import {
  3. DEMO_SESSION_KEY,
  4. type DemoSession,
  5. } from "@/lib/auth-config";
  6. export function readDemoSession(): DemoSession | null {
  7. if (typeof window === "undefined") return null;
  8. try {
  9. const raw = window.localStorage.getItem(DEMO_SESSION_KEY);
  10. if (!raw) return null;
  11. return JSON.parse(raw) as DemoSession;
  12. } catch {
  13. return null;
  14. }
  15. }
  16. export function writeDemoSession(session: DemoSession): void {
  17. window.localStorage.setItem(DEMO_SESSION_KEY, JSON.stringify(session));
  18. window.dispatchEvent(new Event("krypco-auth-change"));
  19. }
  20. export function clearDemoSession(): void {
  21. window.localStorage.removeItem(DEMO_SESSION_KEY);
  22. window.dispatchEvent(new Event("krypco-auth-change"));
  23. }
  24. export function createDemoEmailSession(email: string): DemoSession {
  25. return {
  26. kind: "demo",
  27. userId: `demo_${email.toLowerCase().replace(/[^a-z0-9]/g, "_")}`,
  28. label: email,
  29. createdAt: new Date().toISOString(),
  30. };
  31. }
  32. export function createDemoWalletSession(address: string): DemoSession {
  33. const short = `${address.slice(0, 6)}…${address.slice(-4)}`;
  34. return {
  35. kind: "wallet",
  36. userId: `wallet_${address.toLowerCase()}`,
  37. label: short,
  38. walletAddress: address,
  39. createdAt: new Date().toISOString(),
  40. };
  41. }