| 1 |
import { useState } from 'react' |
| 2 |
import FormErrors from '../components/FormErrors' |
| 3 |
import { getPasswordReset, resetPassword } from '../lib/allauth' |
| 4 |
import { Navigate, Link, useLocation, useLoaderData } from 'react-router-dom' |
| 5 |
import Button from '../components/Button' |
| 6 |
|
| 7 |
export async function resetPasswordByLinkLoader ({ params }) { |
| 8 |
const key = params.key |
| 9 |
const resp = await getPasswordReset(key) |
| 10 |
return { resetKey: key, resetKeyResponse: resp } |
| 11 |
} |
| 12 |
|
| 13 |
function ResetPassword ({ resetKey, resetKeyResponse }) { |
| 14 |
const [password1, setPassword1] = useState('') |
| 15 |
const [password2, setPassword2] = useState('') |
| 16 |
const [password2Errors, setPassword2Errors] = useState([]) |
| 17 |
|
| 18 |
const [response, setResponse] = useState({ fetching: false, content: null }) |
| 19 |
|
| 20 |
function submit () { |
| 21 |
if (password2 !== password1) { |
| 22 |
setPassword2Errors([{ param: 'password2', message: 'Password does not match.' }]) |
| 23 |
return |
| 24 |
} |
| 25 |
setPassword2Errors([]) |
| 26 |
setResponse({ ...response, fetching: true }) |
| 27 |
resetPassword({ key: resetKey, password: password1 }).then((resp) => { |
| 28 |
setResponse((r) => { return { ...r, content: resp } }) |
| 29 |
}).catch((e) => { |
| 30 |
console.error(e) |
| 31 |
window.alert(e) |
| 32 |
}).then(() => { |
| 33 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 34 |
}) |
| 35 |
} |
| 36 |
if ([200, 401].includes(response.content?.status)) { |
| 37 |
return <Navigate to='/account/login' /> |
| 38 |
} |
| 39 |
let body |
| 40 |
if (resetKeyResponse.status !== 200) { |
| 41 |
body = <FormErrors param='key' errors={resetKeyResponse.errors} /> |
| 42 |
} else if (response.content?.errors?.filter(e => e.param === 'key')) { |
| 43 |
body = <FormErrors param='key' errors={response.content?.errors} /> |
| 44 |
} else { |
| 45 |
body = ( |
| 46 |
<> |
| 47 |
<div><label>Password: <input autoComplete='new-password' value={password1} onChange={(e) => setPassword1(e.target.value)} type='password' required /></label> |
| 48 |
<FormErrors param='password' errors={response.content?.errors} /> |
| 49 |
</div> |
| 50 |
<div><label>Password (again): <input value={password2} onChange={(e) => setPassword2(e.target.value)} type='password' required /></label> |
| 51 |
<FormErrors param='password2' errors={password2Errors} /> |
| 52 |
</div> |
| 53 |
|
| 54 |
<Button disabled={response.fetching} onClick={() => submit()}>Reset</Button> |
| 55 |
</> |
| 56 |
) |
| 57 |
} |
| 58 |
|
| 59 |
return ( |
| 60 |
<div> |
| 61 |
<h1>Reset Password</h1> |
| 62 |
<p> |
| 63 |
Remember your password? <Link to='/account/login'>Back to login.</Link> |
| 64 |
</p> |
| 65 |
{body} |
| 66 |
</div> |
| 67 |
) |
| 68 |
} |
| 69 |
|
| 70 |
export function ResetPasswordByLink () { |
| 71 |
const { resetKey, resetKeyResponse } = useLoaderData() |
| 72 |
return <ResetPassword resetKey={resetKey} resetKeyResponse={resetKeyResponse} /> |
| 73 |
} |
| 74 |
|
| 75 |
export function ResetPasswordByCode () { |
| 76 |
const { state } = useLocation() |
| 77 |
if (!state || !state.resetKey || !state.resetKeyResponse) { |
| 78 |
return <Navigate to='/account/password/reset' /> |
| 79 |
} |
| 80 |
return <ResetPassword resetKey={state.resetKey} resetKeyResponse={state.resetKeyResponse} /> |
| 81 |
} |