import { useState } from 'react'
import { useGately } from '@gately/sdk'
function MagicLinkForm() {
const { sendMagicLink } = useGately()
const [email, setEmail] = useState('')
const [sent, setSent] = useState(false)
const [error, setError] = useState('')
const handleSubmit = async (e) => {
e.preventDefault()
setError('')
try {
await sendMagicLink(email, { redirectTo: '/dashboard' })
setSent(true)
} catch (err) {
setError(err.message)
}
}
if (sent) {
return (
<div className="success">
<h2>Check your email</h2>
<p>We sent a login link to {email}</p>
<button onClick={() => setSent(false)}>
Use a different email
</button>
</div>
)
}
return (
<form onSubmit={handleSubmit}>
{error && <p className="error">{error}</p>}
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter your email"
required
/>
<button type="submit">
Send Magic Link
</button>
</form>
)
}