- Phase 3: new migration 20260824000000_redesign.sql (contacts/events/registrations/sponsors, slot types, session speakers + feedback/material), seed rewrite - Phase 4: APPE Admin rebrand + Contacts/Registrations/Sponsors/Schedule/Activity/Events views - Phase 5: Convivo App (24h login, 5s polling, QR type badge, red dot, speaker material) - Phase 6: APPE favicon + PWA icons for both apps - Docs: schema/rpcs/rls/seed/screens/api-client/admin-app/architecture/next-steps/README/roadmap updated 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]>
80 lines
2.6 KiB
TypeScript
80 lines
2.6 KiB
TypeScript
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 { getMyContact, getMyRegistration } from '../lib/api';
|
|
import type { Contact, Registration } from '../lib/types';
|
|
|
|
interface AuthContextValue {
|
|
session: SbSession | null;
|
|
user: User | null;
|
|
contact: Contact | null;
|
|
registration: Registration | null;
|
|
loading: boolean;
|
|
refresh: () => 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 [contact, setContact] = useState<Contact | null>(null);
|
|
const [registration, setRegistration] = useState<Registration | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const load = useCallback(async (email: string) => {
|
|
try {
|
|
const [c, r] = await Promise.all([getMyContact(email), getMyRegistration(email)]);
|
|
setContact(c);
|
|
setRegistration(r);
|
|
} catch (e) {
|
|
console.error('Failed to load contact', e);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
let active = true;
|
|
supabase.auth.getSession().then(({ data }) => {
|
|
if (!active) return;
|
|
setSession(data.session);
|
|
if (data.session?.user?.email) void load(data.session.user.email);
|
|
setLoading(false);
|
|
});
|
|
const { data: sub } = supabase.auth.onAuthStateChange((_event, newSession) => {
|
|
setSession(newSession);
|
|
if (newSession?.user?.email) void load(newSession.user.email);
|
|
else {
|
|
setContact(null);
|
|
setRegistration(null);
|
|
}
|
|
});
|
|
return () => {
|
|
active = false;
|
|
sub.subscription.unsubscribe();
|
|
};
|
|
}, [load]);
|
|
|
|
const refresh = useCallback(async () => {
|
|
if (session?.user?.email) await load(session.user.email);
|
|
}, [session, load]);
|
|
|
|
const signOut = useCallback(async () => {
|
|
await supabase.auth.signOut();
|
|
setContact(null);
|
|
setRegistration(null);
|
|
}, []);
|
|
|
|
const value = useMemo<AuthContextValue>(
|
|
() => ({ session, user: session?.user ?? null, contact, registration, loading, refresh, signOut }),
|
|
[session, contact, registration, loading, refresh, 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;
|
|
}
|