import '@/index.css' import { useState } from 'react' import { getCsrfToken } from '@rudderjs/middleware/client' // URL this view is served at — see Login.tsx for rationale. export const route = '/forgot-password' export interface ForgotPasswordProps { submitUrl?: string loginUrl?: string resetPasswordUrl?: string } export default function ForgotPassword(props: ForgotPasswordProps) { const submitUrl = props.submitUrl ?? '/auth/request-password-reset' const loginUrl = props.loginUrl ?? '/login' const resetPasswordUrl = props.resetPasswordUrl ?? '/reset-password' const [email, setEmail] = useState('') const [error, setError] = useState('') const [success, setSuccess] = useState('') const [loading, setLoading] = useState(false) async function handleSubmit(e: React.FormEvent) { e.preventDefault() setError('') setSuccess('') setLoading(true) try { const res = await fetch(submitUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': getCsrfToken(), }, body: JSON.stringify({ email, redirectTo: resetPasswordUrl }), }) if (res.ok) { setSuccess('If an account exists with that email, a password reset link has been sent.') } else { const body = await res.json().catch(() => ({})) as { message?: string } setError(body.message ?? 'Something went wrong. Please try again.') } } catch { setError('Something went wrong. Please try again.') } setLoading(false) } return (
Enter your email to receive a reset link