Files
pwa/src/atoms/auth.ts
T
ar.badieiandCursor 370ac15471 Ship Novya PWA with backend for deployable production use.
Fix production TypeScript builds and add DEPLOY.md so the app can be hosted from this repo.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-03 21:01:52 +03:30

49 lines
1.3 KiB
TypeScript

import { atom } from 'jotai';
export type BackendUser = {
id: string;
email: string | null;
isGuest: boolean;
profileComplete: boolean;
};
export type GuestSession = {
id: string;
isGuest: true;
createdAt: string;
};
export const GUEST_SESSION_STORAGE_KEY = 'guest-session';
const readGuestSession = (): GuestSession | null => {
if (typeof window === 'undefined') return null;
const raw = localStorage.getItem(GUEST_SESSION_STORAGE_KEY);
if (!raw) return null;
try {
const parsed = JSON.parse(raw) as GuestSession;
if (!parsed?.id || parsed?.isGuest !== true) return null;
return parsed;
} catch {
return null;
}
};
export const backendUserAtom = atom<BackendUser | null>(null);
export const isAuthenticatedAtom = atom<boolean>((get) => {
return !!get(backendUserAtom);
});
export const guestSessionAtom = atom<GuestSession | null>(readGuestSession());
export const guestSessionHydratedAtom = atom(false);
export const isGuestAtom = atom<boolean>((get) => !!get(guestSessionAtom));
export const effectiveUserIdAtom = atom<string | null>((get) => {
const user = get(backendUserAtom);
if (user?.id) return user.id;
const guest = get(guestSessionAtom);
return guest?.id ?? null;
});
export const authInitialCheckDoneAtom = atom(false);