feat: add magic-link admin login and speaker materials portal
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
0b6d1f2b6f
commit
68fecc2ad1
28 files changed
+1623
-232
No files matched your search
@@ -1,16 +1,12 @@
|
||||
import { createContext, useContext, useEffect, useState, type ReactNode } from 'react';
|
||||
import { supabase } from '../lib/supabase';
|
||||
import { changeMyPassword } from '../lib/api';
|
||||
import type { Contact } from '../lib/types';
|
||||
|
||||
interface AuthState {
|
||||
contact: Contact | null;
|
||||
loading: boolean;
|
||||
/** Sign in with the admin's email + password (event-independent). */
|
||||
signIn: (email: string, password: string) => Promise<void>;
|
||||
authError: string | null;
|
||||
signOut: () => Promise<void>;
|
||||
/** Change the current admin's own login password (re-authenticates). */
|
||||
changePassword: (newPassword: string) => Promise<void>;
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthState | null>(null);
|
||||
@@ -33,24 +29,71 @@ async function loadAdminContact(email: string): Promise<Contact | null> {
|
||||
return isAdminContact(c) ? c : null;
|
||||
}
|
||||
|
||||
const callbackUrl = window.location.pathname === '/auth/callback' ? new URL(window.location.href) : null;
|
||||
if (callbackUrl) window.history.replaceState(null, '', '/');
|
||||
let callbackPending = !!callbackUrl;
|
||||
let callbackResult: Promise<{ contact: Contact | null; error: string | null }> | null = null;
|
||||
|
||||
function consumeCallback() {
|
||||
if (!callbackResult) callbackResult = (async () => {
|
||||
const invalidLink = 'This sign-in link is invalid or expired. Please request a new link.';
|
||||
try {
|
||||
const hash = new URLSearchParams(callbackUrl!.hash.slice(1));
|
||||
const query = callbackUrl!.searchParams;
|
||||
if (hash.has('error') || hash.has('error_code') || query.has('error') || query.has('error_code')) {
|
||||
throw new Error(invalidLink);
|
||||
}
|
||||
const accessToken = hash.get('access_token');
|
||||
const refreshToken = hash.get('refresh_token');
|
||||
const code = query.get('code');
|
||||
const result = accessToken && refreshToken
|
||||
? await supabase.auth.setSession({ access_token: accessToken, refresh_token: refreshToken })
|
||||
: code ? await supabase.auth.exchangeCodeForSession(code) : null;
|
||||
if (!result || result.error || !result.data.session?.user.email) throw new Error(invalidLink);
|
||||
const admin = await loadAdminContact(result.data.session.user.email);
|
||||
if (!admin) throw new Error('This account is not an admin.');
|
||||
return { contact: admin, error: null };
|
||||
} catch (error) {
|
||||
await supabase.auth.signOut({ scope: 'local' });
|
||||
return { contact: null, error: error instanceof Error && error.message === 'This account is not an admin.' ? error.message : invalidLink };
|
||||
} finally {
|
||||
callbackPending = false;
|
||||
}
|
||||
})();
|
||||
return callbackResult;
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [contact, setContact] = useState<Contact | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [authError, setAuthError] = useState<string | null>(null);
|
||||
|
||||
// On mount: restore any existing session and load the contact. If the
|
||||
// session belongs to a non-admin, sign out immediately (admin-only app).
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
let revision = 0;
|
||||
|
||||
async function init() {
|
||||
if (callbackUrl) {
|
||||
const result = await consumeCallback();
|
||||
if (!cancelled) {
|
||||
setContact(result.contact);
|
||||
setAuthError(result.error);
|
||||
setLoading(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const currentRevision = revision;
|
||||
const { data } = await supabase.auth.getSession();
|
||||
if (cancelled || currentRevision !== revision) return;
|
||||
const session = data.session;
|
||||
if (!session) {
|
||||
if (!cancelled) setLoading(false);
|
||||
return;
|
||||
}
|
||||
const c = session.user.email ? await loadAdminContact(session.user.email) : null;
|
||||
if (cancelled) return;
|
||||
if (cancelled || currentRevision !== revision) return;
|
||||
if (c) {
|
||||
setContact(c);
|
||||
} else {
|
||||
@@ -62,18 +105,28 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
init();
|
||||
|
||||
const { data: sub } = supabase.auth.onAuthStateChange(async (_event, session) => {
|
||||
const { data: sub } = supabase.auth.onAuthStateChange((event, session) => {
|
||||
if (cancelled || callbackPending || event === 'INITIAL_SESSION') return;
|
||||
const currentRevision = ++revision;
|
||||
if (!session) {
|
||||
setContact(null);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
const c = session.user.email ? await loadAdminContact(session.user.email) : null;
|
||||
if (c) {
|
||||
setContact(c);
|
||||
} else {
|
||||
await supabase.auth.signOut();
|
||||
setContact(null);
|
||||
}
|
||||
window.setTimeout(() => {
|
||||
void (async () => {
|
||||
if (cancelled || currentRevision !== revision) return;
|
||||
const c = session.user.email ? await loadAdminContact(session.user.email) : null;
|
||||
if (cancelled || currentRevision !== revision) return;
|
||||
if (c) {
|
||||
setContact(c);
|
||||
} else {
|
||||
setContact(null);
|
||||
await supabase.auth.signOut();
|
||||
}
|
||||
if (!cancelled) setLoading(false);
|
||||
})();
|
||||
}, 0);
|
||||
});
|
||||
|
||||
return () => {
|
||||
@@ -82,37 +135,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
};
|
||||
}, []);
|
||||
|
||||
async function signIn(email: string, password: string) {
|
||||
const { data, error } = await supabase.auth.signInWithPassword({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
if (error) throw new Error(error.message);
|
||||
|
||||
const c = data.user.email ? await loadAdminContact(data.user.email) : null;
|
||||
if (!c) {
|
||||
// Signed in, but not an admin — reject and sign out.
|
||||
await supabase.auth.signOut();
|
||||
throw new Error('This account is not an admin.');
|
||||
}
|
||||
setContact(c);
|
||||
}
|
||||
|
||||
async function signOut() {
|
||||
await supabase.auth.signOut();
|
||||
setContact(null);
|
||||
}
|
||||
|
||||
async function changePassword(newPassword: string) {
|
||||
if (!contact) throw new Error('Not signed in');
|
||||
await changeMyPassword(contact.email, newPassword);
|
||||
// Re-load the contact so the (now changed) password is reflected.
|
||||
const c = await loadAdminContact(contact.email);
|
||||
if (c) setContact(c);
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ contact, loading, signIn, signOut, changePassword }}>
|
||||
<AuthContext.Provider value={{ contact, loading, authError, signOut }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user