feat: add magic-link admin login and speaker materials portal
This commit is contained in:
1 parent
3adc0fd58f
commit
8d8d3ae4b8
28 files changed
+1623
-232
No files matched your search
@@ -1,23 +1,48 @@
|
||||
import { useState, type FormEvent } from 'react';
|
||||
import { useEffect, useState, type FormEvent } from 'react';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
import { useToast } from '../context/ToastContext';
|
||||
import { supabase } from '../lib/supabase';
|
||||
|
||||
export function Login() {
|
||||
const { signIn } = useAuth();
|
||||
const { authError } = useAuth();
|
||||
const { push } = useToast();
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(authError);
|
||||
const [sent, setSent] = useState(false);
|
||||
const [cooldown, setCooldown] = useState(0);
|
||||
const [resendAt, setResendAt] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
if (!resendAt) return;
|
||||
const timer = window.setInterval(() => {
|
||||
const remaining = Math.max(0, Math.ceil((resendAt - Date.now()) / 1000));
|
||||
setCooldown(remaining);
|
||||
if (!remaining) window.clearInterval(timer);
|
||||
}, 1000);
|
||||
return () => window.clearInterval(timer);
|
||||
}, [resendAt]);
|
||||
|
||||
async function onSubmit(e: FormEvent) {
|
||||
e.preventDefault();
|
||||
if (!email.trim() || !password.trim()) return;
|
||||
if (busy || !email.trim() || cooldown) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
await signIn(email.trim(), password);
|
||||
push('success', 'Welcome back');
|
||||
setSent(false);
|
||||
const { error: sendError } = await supabase.auth.signInWithOtp({
|
||||
email: email.trim(),
|
||||
options: {
|
||||
shouldCreateUser: false,
|
||||
emailRedirectTo: new URL('/auth/callback', window.location.origin).href,
|
||||
},
|
||||
});
|
||||
if (sendError && (sendError.status === 429 || !sendError.status || sendError.status >= 500)) {
|
||||
throw new Error('Unable to send a sign-in link right now. Please try again later.');
|
||||
}
|
||||
setSent(true);
|
||||
setCooldown(60);
|
||||
setResendAt(Date.now() + 60_000);
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : 'Sign in failed';
|
||||
setError(msg);
|
||||
@@ -34,37 +59,28 @@ export function Login() {
|
||||
<div className="logo"><img src="/icon-192.png" alt="APPE" /></div>
|
||||
<div>
|
||||
<h1>APPE Admin</h1>
|
||||
<p>Sign in with your admin email and password.</p>
|
||||
<p>Sign in to your admin account.</p>
|
||||
</div>
|
||||
</div>
|
||||
<form className="login-form" onSubmit={onSubmit}>
|
||||
<div className="field">
|
||||
<label>Email</label>
|
||||
<label htmlFor="login-email">Email</label>
|
||||
<input
|
||||
id="login-email"
|
||||
className="input"
|
||||
type="email"
|
||||
placeholder="[email protected]"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
onChange={(e) => { setEmail(e.target.value); setSent(false); }}
|
||||
autoComplete="username"
|
||||
disabled={busy}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="field">
|
||||
<label>Password</label>
|
||||
<input
|
||||
className="input"
|
||||
type="password"
|
||||
placeholder="••••••••••"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
autoComplete="current-password"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
{error && <div className="login-err">{error}</div>}
|
||||
<button className="btn primary" type="submit" disabled={busy}>
|
||||
{busy ? 'Signing in…' : 'Sign in'}
|
||||
{sent && <div role="status">If this email belongs to an eligible account, a sign-in link has been sent.</div>}
|
||||
{error && <div className="login-err" role="alert">{error}</div>}
|
||||
<button className="btn primary" type="submit" disabled={busy || cooldown > 0}>
|
||||
{busy ? 'Sending...' : cooldown ? `Resend in ${cooldown}s` : sent ? 'Resend magic link' : 'Send magic link'}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user