Files
pterodactyl-panel/resources/scripts/components/auth/ForgotPasswordContainer.tsx
T

82 lines
3.0 KiB
TypeScript
Raw Normal View History

2026-10-05 18:10:18 -04:00
import { useRef } from 'react';
import { Link } from '@tanstack/react-router';
2019-06-22 13:53:41 -07:00
import LoginFormContainer from '@/components/auth/LoginFormContainer';
2026-10-05 18:10:18 -04:00
import { useAppForm } from '@/components/form';
import type { InvisibleRecaptchaHandle } from '@/components/elements/InvisibleRecaptcha';
import InvisibleRecaptcha from '@/components/elements/InvisibleRecaptcha';
import { useSiteSettings } from '@/api/settings/queries';
import { useRequestPasswordResetEmail } from '@/api/auth/queries';
import { toast } from 'sonner';
2019-06-11 22:02:18 -07:00
2026-10-05 18:10:18 -04:00
const isEmail = (value: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
2026-10-05 18:10:18 -04:00
export default function ForgotPasswordContainer() {
const recaptchaRef = useRef<InvisibleRecaptchaHandle>(null);
2026-10-05 18:10:18 -04:00
const requestReset = useRequestPasswordResetEmail();
const { enabled: recaptchaEnabled, siteKey } = useSiteSettings().recaptcha;
2026-10-05 18:10:18 -04:00
const form = useAppForm({
defaultValues: { email: '' },
onSubmit: async ({ value }) => {
let token = '';
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
if (recaptchaEnabled) {
token = (await recaptchaRef.current?.execute()) ?? '';
if (!token) {
toast.error('Captcha verification failed, please try again.');
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
return;
}
}
2026-10-05 18:10:18 -04:00
try {
await requestReset.mutateAsync({
body: {
email: value.email,
'g-recaptcha-response': token,
},
});
form.reset();
} catch {
// Error toast is handled by the mutation.
}
},
});
2019-06-11 22:02:18 -07:00
return (
2026-10-08 16:53:20 -04:00
<LoginFormContainer form={form} title='Request Password Reset' className='w-full flex'>
2026-10-05 18:10:18 -04:00
<form.AppField
2026-10-08 16:53:20 -04:00
name='email'
2026-10-05 18:10:18 -04:00
validators={{
onChange: ({ value }) =>
isEmail(value) ? undefined : 'A valid email address must be provided to continue.',
}}
>
{(field) => (
<field.TextField
2020-07-04 14:21:28 -07:00
light
2026-10-08 16:53:20 -04:00
type='email'
label='Email'
description='Enter your account email address to receive instructions on resetting your password.'
/>
2026-10-05 18:10:18 -04:00
)}
</form.AppField>
2026-10-08 16:53:20 -04:00
<div className='mt-6'>
2026-10-05 18:10:18 -04:00
<form.AppForm>
2026-10-08 16:53:20 -04:00
<form.SubmitButton size='xlarge'>Send Email</form.SubmitButton>
2026-10-05 18:10:18 -04:00
</form.AppForm>
</div>
{recaptchaEnabled && <InvisibleRecaptcha ref={recaptchaRef} siteKey={siteKey || ''} />}
2026-10-08 16:53:20 -04:00
<div className='mt-6 text-center'>
2026-10-05 18:10:18 -04:00
<Link
2026-10-08 16:53:20 -04:00
to='/auth/login'
className='text-xs text-muted-foreground tracking-wide uppercase no-underline hover:text-foreground'
2026-10-05 18:10:18 -04:00
>
Return to Login
</Link>
</div>
</LoginFormContainer>
);
2026-10-05 18:10:18 -04:00
}