Phase 0 + 1: Dockerized Supabase core + attendee PWA
Phase 0 — Supabase core (Docker via CLI): - Schema: profiles, slots, sessions, preferences, connections, checkins, feedback, leaderboard (Realtime mirror), points_log - RLS on every table; transactional security-definer points RPCs - Seed: 6 slots, 18 sessions, 13 demo users (QR token as auth password) Phase 1 — Attendee web PWA (React + Vite + TS): - Login via QR token + email (GoTrue, no email sent) - Home, Agenda, Scan/Connect, staff Check-in, Feedback, live Leaderboard, Profile - Same-origin proxy to Supabase (Vite dev / nginx in Docker) - Docker: multi-stage build -> nginx sidecar on the Supabase network
This commit is contained in:
commit
37684ba116
45 files changed
+6922
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useMemo, useState, type ReactNode } from 'react';
|
||||
import type { Session as SbSession, User } from '@supabase/supabase-js';
|
||||
import { supabase } from '../lib/supabase';
|
||||
import type { Profile } from '../lib/types';
|
||||
|
||||
interface AuthContextValue {
|
||||
session: SbSession | null;
|
||||
user: User | null;
|
||||
profile: Profile | null;
|
||||
loading: boolean;
|
||||
refreshProfile: () => Promise<void>;
|
||||
signOut: () => Promise<void>;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [session, setSession] = useState<SbSession | null>(null);
|
||||
const [profile, setProfile] = useState<Profile | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const loadProfile = useCallback(async (userId: string) => {
|
||||
const { data, error } = await supabase.from('profiles').select('*').eq('id', userId).maybeSingle();
|
||||
if (error) {
|
||||
console.error('Failed to load profile', error);
|
||||
return;
|
||||
}
|
||||
setProfile((data as Profile) ?? null);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
supabase.auth.getSession().then(({ data }) => {
|
||||
if (!active) return;
|
||||
setSession(data.session);
|
||||
if (data.session?.user) void loadProfile(data.session.user.id);
|
||||
setLoading(false);
|
||||
});
|
||||
const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
|
||||
setSession(newSession);
|
||||
if (newSession?.user) void loadProfile(newSession.user.id);
|
||||
else setProfile(null);
|
||||
});
|
||||
return () => {
|
||||
active = false;
|
||||
sub.subscription.unsubscribe();
|
||||
};
|
||||
}, [loadProfile]);
|
||||
|
||||
const refreshProfile = useCallback(async () => {
|
||||
if (session?.user) await loadProfile(session.user.id);
|
||||
}, [session, loadProfile]);
|
||||
|
||||
const signOut = useCallback(async () => {
|
||||
await supabase.auth.signOut();
|
||||
setProfile(null);
|
||||
}, []);
|
||||
|
||||
const value = useMemo<AuthContextValue>(
|
||||
() => ({ session, user: session?.user ?? null, profile, loading, refreshProfile, signOut }),
|
||||
[session, profile, loading, refreshProfile, signOut],
|
||||
);
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAuth(): AuthContextValue {
|
||||
const ctx = useContext(AuthContext);
|
||||
if (!ctx) throw new Error('useAuth must be used within AuthProvider');
|
||||
return ctx;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { createContext, useCallback, useContext, useRef, useState, type ReactNode } from 'react';
|
||||
|
||||
type ToastType = '' | 'ok' | 'err';
|
||||
interface ToastContextValue {
|
||||
toast: (msg: string, type?: ToastType) => void;
|
||||
}
|
||||
|
||||
const ToastContext = createContext<ToastContextValue | null>(null);
|
||||
|
||||
export function ToastProvider({ children }: { children: ReactNode }) {
|
||||
const [msg, setMsg] = useState('');
|
||||
const [type, setType] = useState<ToastType>('');
|
||||
const [show, setShow] = useState(false);
|
||||
const timer = useRef<number | undefined>(undefined);
|
||||
|
||||
const toast = useCallback((text: string, t: ToastType = '') => {
|
||||
setMsg(text);
|
||||
setType(t);
|
||||
setShow(true);
|
||||
window.clearTimeout(timer.current);
|
||||
timer.current = window.setTimeout(() => setShow(false), 2600);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={{ toast }}>
|
||||
{children}
|
||||
<div className={`toast ${show ? 'show' : ''} ${type}`}>{msg}</div>
|
||||
</ToastContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useToast(): ToastContextValue {
|
||||
const ctx = useContext(ToastContext);
|
||||
if (!ctx) throw new Error('useToast must be used within ToastProvider');
|
||||
return ctx;
|
||||
}
|
||||
Reference in new issue
Block a user