| 1 |
import { useState } from 'react' |
| 2 |
import { |
| 3 |
useLoaderData, |
| 4 |
Navigate |
| 5 |
} from 'react-router-dom' |
| 6 |
import { getEmailVerification, verifyEmail } from '../lib/allauth' |
| 7 |
import Button from '../components/Button' |
| 8 |
|
| 9 |
export async function loader ({ params }) { |
| 10 |
const key = params.key |
| 11 |
const resp = await getEmailVerification(key) |
| 12 |
return { key, verification: resp } |
| 13 |
} |
| 14 |
|
| 15 |
export default function VerifyEmail () { |
| 16 |
const { key, verification } = useLoaderData() |
| 17 |
const [response, setResponse] = useState({ fetching: false, content: null }) |
| 18 |
|
| 19 |
function submit () { |
| 20 |
setResponse({ ...response, fetching: true }) |
| 21 |
verifyEmail(key).then((content) => { |
| 22 |
setResponse((r) => { return { ...r, content } }) |
| 23 |
}).catch((e) => { |
| 24 |
console.error(e) |
| 25 |
window.alert(e) |
| 26 |
}).then(() => { |
| 27 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 28 |
}) |
| 29 |
} |
| 30 |
|
| 31 |
if ([200, 401].includes(response.content?.status)) { |
| 32 |
return <Navigate to='/account/email' /> |
| 33 |
} |
| 34 |
|
| 35 |
let body = null |
| 36 |
if (verification.status === 200) { |
| 37 |
body = ( |
| 38 |
<> |
| 39 |
<p>Please confirm that <a href={'mailto:' + verification.data.email}>{verification.data.email}</a> is an email address for user {verification.data.user.str}.</p> |
| 40 |
<Button disabled={response.fetching} onClick={() => submit()}>Confirm</Button> |
| 41 |
</> |
| 42 |
) |
| 43 |
} else if (!verification.data?.email) { |
| 44 |
body = <p>Invalid verification link.</p> |
| 45 |
} else { |
| 46 |
body = <p>Unable to confirm email <a href={'mailto:' + verification.data.email}>{verification.data.email}</a> because it is already confirmed.</p> |
| 47 |
} |
| 48 |
return ( |
| 49 |
<div> |
| 50 |
<h1>Confirm Email Address</h1> |
| 51 |
{body} |
| 52 |
</div> |
| 53 |
) |
| 54 |
} |