import { useEffect, useState } from 'react'; import { msg } from '@lingui/core/macro'; import { useLingui } from '@lingui/react'; import { Trans } from '@lingui/react/macro'; import { AlertTriangle, CheckCircle2, Loader, XCircle } from 'lucide-react'; import { Link, redirect, useNavigate } from 'react-router'; import { match } from 'ts-pattern'; import { authClient } from '@documenso/auth/client'; import { useOptionalSession } from '@documenso/lib/client-only/providers/session'; import { EMAIL_VERIFICATION_STATE } from '@documenso/lib/constants/email'; import { Button } from '@documenso/ui/primitives/button'; import { useToast } from '@documenso/ui/primitives/use-toast'; import type { Route } from './+types/verify-email.$token'; export const loader = ({ params }: Route.LoaderArgs) => { const { token } = params; if (!token) { throw redirect('/verify-email'); } return { token, }; }; export default function VerifyEmailPage({ loaderData }: Route.ComponentProps) { const { token } = loaderData; const { refreshSession } = useOptionalSession(); const { _ } = useLingui(); const { toast } = useToast(); const navigate = useNavigate(); const [state, setState] = useState(null); const [isLoading, setIsLoading] = useState(false); const verifyToken = async () => { setIsLoading(true); try { const response = await authClient.emailPassword.verifyEmail({ token, }); await refreshSession(); setState(response.state); } catch (err) { console.error(err); toast({ title: _(msg`Something went wrong`), description: _(msg`We were unable to verify your email at this time.`), }); await navigate('/verify-email'); } setIsLoading(false); }; useEffect(() => { void verifyToken(); }, []); if (isLoading || state === null) { return (
); } return match(state) .with(EMAIL_VERIFICATION_STATE.NOT_FOUND, () => (

Something went wrong

We were unable to verify your email. If your email is not verified already, please try again.

)) .with(EMAIL_VERIFICATION_STATE.EXPIRED, () => (

Your token has expired!

It seems that the provided token has expired. We've just sent you another token, please check your email and try again.

)) .with(EMAIL_VERIFICATION_STATE.VERIFIED, () => (

Email Confirmed!

Your email has been successfully confirmed! You can now use all features of Documenso.

)) .with(EMAIL_VERIFICATION_STATE.ALREADY_VERIFIED, () => (

Email already confirmed

Your email has already been confirmed. You can now use all features of Documenso.

)) .exhaustive(); }