// Login screen: first-run main-admin setup, plus Sign In / Sign Up tabs. Themed via CSS variables. function LoginScreen({ accounts, onAuth, churchName }){ const firstRun = !accounts; const [mode, setMode] = React.useState('login'); // 'login' | 'signup' const [name, setName] = React.useState(''); const [email, setEmail] = React.useState(firstRun ? window.SUPER_ADMIN_EMAIL : ''); const [pw, setPw] = React.useState(''); const [pw2, setPw2] = React.useState(''); const [err, setErr] = React.useState(''); const signup = !firstRun && mode === 'signup'; function submit(e){ e.preventDefault(); setErr(''); if(firstRun){ if(pw.length < 4){ setErr('Choose a password of at least 4 characters.'); return; } if(pw !== pw2){ setErr('Passwords do not match.'); return; } const a = window.initAccounts(pw); window.setSession(a.users[0].email); onAuth(a, a.users[0]); return; } if(signup){ const em = email.trim().toLowerCase(); if(!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(em)){ setErr('Enter a valid email address.'); return; } if(window.findUser(accounts, em)){ setErr('An account with that email already exists. Please sign in.'); return; } if(pw.length < 4){ setErr('Choose a password of at least 4 characters.'); return; } if(pw !== pw2){ setErr('Passwords do not match.'); return; } const newUser = { email: em, name: name.trim() || em.split('@')[0], role: 'user', password: pw }; const next = { ...accounts, users: [...accounts.users, newUser] }; window.saveAccounts(next); window.setSession(em); onAuth(next, newUser); return; } // login const u = window.verifyLogin(accounts, email, pw); if(!u){ setErr('Incorrect email or password.'); return; } window.setSession(u.email); onAuth(accounts, u); } return (