| 1 |
import { useState } from 'react' |
| 2 |
import FormErrors from '../components/FormErrors' |
| 3 |
import { requestPasswordReset, Flows } from '../lib/allauth' |
| 4 |
import { Navigate, Link } from 'react-router-dom' |
| 5 |
import Button from '../components/Button' |
| 6 |
|
| 7 |
export default function RequestPasswordReset () { |
| 8 |
const [email, setEmail] = useState('') |
| 9 |
const [response, setResponse] = useState({ fetching: false, content: null }) |
| 10 |
|
| 11 |
function submit () { |
| 12 |
setResponse({ ...response, fetching: true }) |
| 13 |
requestPasswordReset(email).then((content) => { |
| 14 |
setResponse((r) => { return { ...r, content } }) |
| 15 |
}).catch((e) => { |
| 16 |
console.error(e) |
| 17 |
window.alert(e) |
| 18 |
}).then(() => { |
| 19 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 20 |
}) |
| 21 |
} |
| 22 |
|
| 23 |
if (response.content?.status === 401) { |
| 24 |
return <Navigate to='/account/password/reset/confirm' /> |
| 25 |
} |
| 26 |
if (response.content?.status === 200) { |
| 27 |
return ( |
| 28 |
<div> |
| 29 |
<h1>Reset Password</h1> |
| 30 |
<p>Password reset sent.</p> |
| 31 |
</div> |
| 32 |
) |
| 33 |
} |
| 34 |
return ( |
| 35 |
<div> |
| 36 |
<h1>Reset Password</h1> |
| 37 |
<p> |
| 38 |
Remember your password? <Link to='/account/login'>Back to login.</Link> |
| 39 |
</p> |
| 40 |
|
| 41 |
<FormErrors errors={response.content?.errors} /> |
| 42 |
|
| 43 |
<div><label>Email <input value={email} onChange={(e) => setEmail(e.target.value)} type='email' required /></label> |
| 44 |
<FormErrors param='email' errors={response.content?.errors} /> |
| 45 |
</div> |
| 46 |
<Button disabled={response.fetching} onClick={() => submit()}>Reset</Button> |
| 47 |
</div> |
| 48 |
) |
| 49 |
} |