Auth.jsx (1638B)
1 import { useState } from 'react' 2 3 const session_ttl = import.meta.env.VITE_SESSION_TTL 4 const api_uri = import.meta.env.VITE_API_URI 5 const api_port = import.meta.env.VITE_API_PORT 6 7 function Auth() { 8 const [loading, setLoading] = useState(false) 9 const [email, setEmail] = useState('') 10 11 const handleLogin = async (e) => { 12 e.preventDefault() 13 14 try { 15 setLoading(true) 16 17 const response = await fetch(`${api_uri}:${api_port}/v1/auth/requestMagicLink`, { 18 method: 'POST', 19 headers: {'Content-Type': 'application/json'}, 20 body: JSON.stringify({"email": email}), 21 }) 22 23 const { data, error } = await response.json() 24 if (error) { 25 alert(error) 26 } else { 27 alert('Check your email for the login link!') 28 } 29 } catch (error) { 30 alert(error.error_description || error.message) 31 } finally { 32 setLoading(false) 33 } 34 } 35 36 return ( 37 <div className="Auth"> 38 <div> 39 <button disabled={!loading}>{loading ? '🛸 sending magic link ...' : ''}</button> 40 <h1>Login</h1> 41 <p>Sign in with a magic link.</p> 42 <div className='loginForm'> 43 <form onSubmit={handleLogin}> 44 <input 45 id="email" 46 type="email" 47 placeholder="Your email" 48 value={email} 49 onChange={(e) => setEmail(e.target.value)} 50 /><br/> 51 <button>🛸 Send magic link</button> 52 </form> 53 </div> 54 <h1>Registration</h1> 55 <p>No account yet? Contact event administrator to sign up.</p> 56 </div> 57 </div> 58 ) 59 } 60 61 export default Auth;