Phase 2: Admin console (admin.appe.at)
A desktop-first React app for organizers, guarded to the admin role. Backend (supabase/migrations/20260823000000_admin_app.sql): - Admin-only SELECT policies on preferences, connections, feedback - reset_qr_token RPC: regenerates a lanyard token, re-hashes it as the Auth password (old token stops working), and clears the attendee's sessions to force re-login App (apps/admin, port 3001): - Login: same QR+email flow, but non-admin sessions are rejected - Dashboard: live stats + top points + recent activity - Attendees: search, edit name/company/role, reset QR, adjust points - Schedule: manage slots + sessions (create/edit/delete) - Activity: check-ins / connections / feedback tables - Same-origin proxy (Vite dev / nginx in Docker), same design tokens Docker + scripts: - convivo-admin service in docker-compose.yml (:3001) - admin:dev / admin:build root scripts - fixed db:reset script (supabase reset -> supabase db reset) Docs: new frontend/admin-app.md; updated architecture, rls, rpcs, docker, next-steps, and READMEs. Verified: build clean, all views + flows browser-tested, Docker build + proxy + login + admin guard confirmed. DB reset to clean seed. 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:
1 parent
015c0a3a06
commit
32c86767ef
45 files changed
+4425
-87
No files matched your search
@@ -0,0 +1,108 @@
|
||||
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react';
|
||||
import { supabase } from '../lib/supabase';
|
||||
import type { Profile } from '../lib/types';
|
||||
|
||||
interface AuthState {
|
||||
profile: Profile | null;
|
||||
loading: boolean;
|
||||
signIn: (email: string, qrToken: string) => Promise<void>;
|
||||
signOut: () => Promise<void>;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthState | null>(null);
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [profile, setProfile] = useState<Profile | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
// On mount: restore any existing session and load the profile. If the
|
||||
// session belongs to a non-admin, sign out immediately (admin-only app).
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
async function init() {
|
||||
const { data } = await supabase.auth.getSession();
|
||||
const session = data.session;
|
||||
if (!session) {
|
||||
if (!cancelled) setLoading(false);
|
||||
return;
|
||||
}
|
||||
const { data: prof } = await supabase
|
||||
.from('profiles')
|
||||
.select('*')
|
||||
.eq('id', session.user.id)
|
||||
.maybeSingle();
|
||||
if (cancelled) return;
|
||||
if (prof && prof.role === 'admin') {
|
||||
setProfile(prof as Profile);
|
||||
} else {
|
||||
// Not an admin — clear the session.
|
||||
await supabase.auth.signOut();
|
||||
}
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
const { data: sub } = supabase.auth.onAuthStateChange(async (_event, session) => {
|
||||
if (!session) {
|
||||
setProfile(null);
|
||||
return;
|
||||
}
|
||||
const { data: prof } = await supabase
|
||||
.from('profiles')
|
||||
.select('*')
|
||||
.eq('id', session.user.id)
|
||||
.maybeSingle();
|
||||
if (prof && prof.role === 'admin') {
|
||||
setProfile(prof as Profile);
|
||||
} else {
|
||||
await supabase.auth.signOut();
|
||||
setProfile(null);
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
sub.subscription.unsubscribe();
|
||||
};
|
||||
}, []);
|
||||
|
||||
async function signIn(email: string, qrToken: string) {
|
||||
const { data, error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password: qrToken,
|
||||
});
|
||||
if (error) throw new Error(error.message);
|
||||
|
||||
const { data: prof } = await supabase
|
||||
.from('profiles')
|
||||
.select('*')
|
||||
.eq('id', data.user.id)
|
||||
.maybeSingle();
|
||||
|
||||
if (!prof || prof.role !== 'admin') {
|
||||
// Signed in, but not an admin — reject and sign out.
|
||||
await supabase.auth.signOut();
|
||||
throw new Error('This account is not an admin.');
|
||||
}
|
||||
setProfile(prof as Profile);
|
||||
}
|
||||
|
||||
async function signOut() {
|
||||
await supabase.auth.signOut();
|
||||
setProfile(null);
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ profile, loading, signIn, signOut }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useAuth(): AuthState {
|
||||
const ctx = useContext(AuthContext);
|
||||
if (!ctx) throw new Error('useAuth must be used within AuthProvider');
|
||||
return ctx;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { createContext, useCallback, useContext, useState, type ReactNode } from 'react';
|
||||
|
||||
type ToastKind = 'success' | 'error' | 'info';
|
||||
interface Toast {
|
||||
id: number;
|
||||
kind: ToastKind;
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface ToastState {
|
||||
toasts: Toast[];
|
||||
push: (kind: ToastKind, message: string) => void;
|
||||
dismiss: (id: number) => void;
|
||||
}
|
||||
|
||||
const ToastContext = createContext<ToastState | null>(null);
|
||||
let nextId = 1;
|
||||
|
||||
export function ToastProvider({ children }: { children: ReactNode }) {
|
||||
const [toasts, setToasts] = useState<Toast[]>([]);
|
||||
|
||||
const dismiss = useCallback((id: number) => {
|
||||
setToasts((t) => t.filter((x) => x.id !== id));
|
||||
}, []);
|
||||
|
||||
const push = useCallback(
|
||||
(kind: ToastKind, message: string) => {
|
||||
const id = nextId++;
|
||||
setToasts((t) => [...t, { id, kind, message }]);
|
||||
window.setTimeout(() => dismiss(id), 3800);
|
||||
},
|
||||
[dismiss],
|
||||
);
|
||||
|
||||
return (
|
||||
<ToastContext.Provider value={{ toasts, push, dismiss }}>
|
||||
{children}
|
||||
<div className="toast-stack" role="status" aria-live="polite">
|
||||
{toasts.map((t) => (
|
||||
<div key={t.id} className={`toast toast-${t.kind}`} onClick={() => dismiss(t.id)}>
|
||||
{t.message}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</ToastContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useToast(): ToastState {
|
||||
const ctx = useContext(ToastContext);
|
||||
if (!ctx) throw new Error('useToast must be used within ToastProvider');
|
||||
return ctx;
|
||||
}
|
||||
Reference in new issue
Block a user