feat: add magic-link admin login and speaker materials portal

This commit is contained in:
Lago committed 2026-10-07 18:27:56 +02:00
1 parent 3adc0fd58f
commit 8d8d3ae4b8
28 files changed
+1623 -232

No files matched your search

+18 -8
View File
@@ -232,14 +232,23 @@ export async function getMySpeakerSessions(email: string): Promise<string[]> {
* The path is persisted via the `set_session_material` RPC (the sessions table
* is admin-write only, so speakers need the security-definer RPC). */
export async function uploadSessionMaterial(sessionId: string, file: File): Promise<string> {
const path = `sessions/${sessionId}/${Date.now()}-${file.name.replace(/[^a-zA-Z0-9._-]/g, '_')}`;
const { error: upErr } = await supabase.storage.from('materials').upload(path, file, { upsert: true });
const { data: previous, error: readError } = await supabase.from('sessions').select('material_path').eq('id', sessionId).single();
if (readError) throw new ApiError(readError.message);
const path = `sessions/${sessionId}/${crypto.randomUUID()}-${file.name.replace(/[^a-zA-Z0-9._-]/g, '_')}`;
const { error: upErr } = await supabase.storage.from('materials').upload(path, file, { upsert: false });
if (upErr) throw new ApiError(upErr.message);
const { error: dbErr } = await supabase.rpc('set_session_material', {
p_session_id: sessionId,
p_material_path: path,
});
if (dbErr) throw new ApiError(dbErr.message);
if (dbErr) {
const { error: cleanupError } = await supabase.storage.from('materials').remove([path]);
throw new ApiError(`${dbErr.message}${cleanupError ? ' The unattached upload could not be cleaned up.' : ''}`);
}
if (previous.material_path && previous.material_path !== path) {
const { error: cleanupError } = await supabase.storage.from('materials').remove([previous.material_path]);
if (cleanupError) throw new ApiError('Material saved, but the previous file could not be removed. Refresh to see the saved material.');
}
return path;
}
@@ -247,15 +256,16 @@ export async function uploadSessionMaterial(sessionId: string, file: File): Prom
* can delete their own material to re-upload; the path is cleared via the
* `set_session_material` RPC (sessions is admin-write only). */
export async function removeSessionMaterial(sessionId: string): Promise<void> {
const { data } = await supabase.from('sessions').select('material_path').eq('id', sessionId).maybeSingle();
const { data, error: readError } = await supabase.from('sessions').select('material_path').eq('id', sessionId).single();
if (readError) throw new ApiError(readError.message);
const path = (data as Session | null)?.material_path;
if (path) {
const { error: rmErr } = await supabase.storage.from('materials').remove([path]);
if (rmErr) throw new ApiError(rmErr.message);
}
const { error: dbErr } = await supabase.rpc('set_session_material', {
p_session_id: sessionId,
p_material_path: null,
});
if (dbErr) throw new ApiError(dbErr.message);
if (path) {
const { error: rmErr } = await supabase.storage.from('materials').remove([path]);
if (rmErr) throw new ApiError('Material detached, but the file could not be removed from storage.');
}
}
+56
View File
@@ -0,0 +1,56 @@
import type { Session } from '@supabase/supabase-js';
import { supabase } from './supabase';
export const isSpeakerPortal = /^\/speaker(?:\/secret)?\/?$/.test(window.location.pathname);
export interface SpeakerSession {
session_id: string;
title: string;
description: string | null;
room: string | null;
material_path: string | null;
event_id: string;
event_name: string;
event_start_date: string;
event_end_date: string;
is_active: boolean;
slot_date: string;
start_time: string;
end_time: string;
}
async function consumeCallback(): Promise<Session | null> {
const url = new URL(window.location.href);
const hash = new URLSearchParams(url.hash.slice(1));
const accessToken = hash.get('access_token');
const refreshToken = hash.get('refresh_token');
const code = url.searchParams.get('code');
const callbackError = hash.get('error') || url.searchParams.get('error');
const hasCallback = accessToken || refreshToken || code || callbackError;
if (hasCallback) window.history.replaceState(null, '', window.location.pathname);
if (callbackError) throw new Error('This sign-in link has expired or is invalid. Request a new link.');
if (accessToken || refreshToken) {
if (!accessToken || !refreshToken) throw new Error('Incomplete sign-in link. Request a new link.');
const { data, error } = await supabase.auth.setSession({ access_token: accessToken, refresh_token: refreshToken });
if (error) throw new Error('This sign-in link has expired or is invalid. Request a new link.');
return data.session;
}
if (code) {
const { data, error } = await supabase.auth.exchangeCodeForSession(code);
if (error) throw new Error('This sign-in link could not be opened on this device. Request a new link.');
return data.session;
}
const { data, error } = await supabase.auth.getSession();
if (error) throw error;
return data.session;
}
export const speakerPortalSession = isSpeakerPortal
? consumeCallback().then((session) => ({ session, error: null }), (error: Error) => ({ session: null, error }))
: Promise.resolve({ session: null, error: null });
export async function getSpeakerPortalSessions(): Promise<SpeakerSession[]> {
const { data, error } = await supabase.rpc('get_my_speaker_sessions');
if (error) throw new Error(error.message);
return data as SpeakerSession[];
}