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 Co-authored-by: Stephan Charles <[email protected]> Co-authored-by: Amelia Hernandez <[email protected]> Co-authored-by: Markus Franz <[email protected]> Co-authored-by: Sabrina Hota <[email protected]> Co-authored-by: Nilüfer Doğan <[email protected]>
This commit is contained in:
commit
63aa4ecfc8
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;
|
||||
}
|
||||
Reference in new issue
Block a user