| 1 |
import { useState } from 'react' |
| 2 |
import FormErrors from '../components/FormErrors' |
| 3 |
import { getPasswordReset, Flows } from '../lib/allauth' |
| 4 |
import { Navigate } from 'react-router-dom' |
| 5 |
import Button from '../components/Button' |
| 6 |
import { useAuthStatus } from '../auth' |
| 7 |
|
| 8 |
export default function ConfirmPasswordResetCode () { |
| 9 |
const [, authInfo] = useAuthStatus() |
| 10 |
const [code, setCode] = useState('') |
| 11 |
const [response, setResponse] = useState({ fetching: false, content: null }) |
| 12 |
|
| 13 |
function submit () { |
| 14 |
setResponse({ ...response, fetching: true }) |
| 15 |
getPasswordReset(code).then((content) => { |
| 16 |
setResponse((r) => { return { ...r, content } }) |
| 17 |
}).catch((e) => { |
| 18 |
console.error(e) |
| 19 |
window.alert(e) |
| 20 |
}).then(() => { |
| 21 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 22 |
}) |
| 23 |
} |
| 24 |
|
| 25 |
if (response.content?.status === 409 || authInfo.pendingFlow?.id !== Flows.PASSWORD_RESET_BY_CODE) { |
| 26 |
return <Navigate to='/account/password/reset' /> |
| 27 |
} else if (response.content?.status === 200) { |
| 28 |
return <Navigate state={{ resetKey: code, resetKeyResponse: response.content }} to='/account/password/reset/complete' /> |
| 29 |
} |
| 30 |
return ( |
| 31 |
<div> |
| 32 |
<h1>Enter Password Reset Code </h1> |
| 33 |
<p> |
| 34 |
The code expires shortly, so please enter it soon. |
| 35 |
</p> |
| 36 |
|
| 37 |
<FormErrors errors={response.content?.errors} /> |
| 38 |
|
| 39 |
<div><label>Code <input value={code} onChange={(e) => setCode(e.target.value)} type='code' required /></label> |
| 40 |
<FormErrors param='key' errors={response.content?.errors} /> |
| 41 |
</div> |
| 42 |
<Button disabled={response.fetching} onClick={() => submit()}>Confirm</Button> |
| 43 |
</div> |
| 44 |
) |
| 45 |
} |