'use client' import { useState, useEffect } from 'react' import { useSearchParams, useRouter } from 'next/navigation' import Link from 'next/link' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from '@/components/ui/card' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert' import { Loader2, CheckCircle, XCircle, Mail, ArrowRight } from 'lucide-react' import { useMutation, useQuery } from '@tanstack/react-query' import httpBrowserClient from '@/lib/httpBrowserClient' import { ApiEndpoints } from '@/config/api' import { Routes } from '@/config/routes' // Reusable components const ErrorAlert = ({ message }: { message: string }) => ( Error {message} ) const SuccessAlert = ({ title, message }: { title: string; message: string }) => ( {title} {message} ) const InfoAlert = ({ title, message }: { title: string; message: string }) => ( {title} {message} ) const LoadingSpinner = () => (
) const DashboardButton = () => ( ) const LoginButton = () => ( ) export default function VerifyEmailPage() { const router = useRouter() const searchParams = useSearchParams() const userId = searchParams.get('userId') const verificationCode = searchParams.get('verificationCode') const verificationEmailSent = searchParams.get('verificationEmailSent') const [successMessage, setSuccessMessage] = useState('') const [errorMessage, setErrorMessage] = useState('') // Check user authentication and email verification status const { data: whoAmIData, isPending: isCheckingAuth, isError: isAuthError } = useQuery({ queryKey: ['whoAmI'], queryFn: () => httpBrowserClient.get(ApiEndpoints.auth.whoAmI()), retry: 1, }) const user = whoAmIData?.data?.data const isEmailVerified = !!user?.emailVerifiedAt const isLoggedIn = !isAuthError && !!user // Verify email mutation const { mutate: verifyEmail, isPending: isVerifying } = useMutation({ mutationFn: () => httpBrowserClient.post('/auth/verify-email', { userId, verificationCode, }), onSuccess: () => { setSuccessMessage('Your email has been successfully verified') setErrorMessage('') }, onError: (error: any) => { setErrorMessage(error.message || 'Failed to verify email') }, }) // Send verification email mutation const { mutate: sendVerificationEmail, isPending: isSending } = useMutation({ mutationFn: () => httpBrowserClient.post( ApiEndpoints.auth.sendEmailVerificationEmail(), {} ), onSuccess: () => { if (!verificationEmailSent) { router.push('/verify-email?verificationEmailSent=true') } else { setSuccessMessage('Verification email has been sent to your inbox') setErrorMessage('') } }, onError: (error: any) => { setErrorMessage(error.message || 'Failed to send verification email') }, }) // Handle verification when code is provided useEffect(() => { if (userId && verificationCode && !isVerifying && !successMessage && !errorMessage) { if (isEmailVerified) { setSuccessMessage('Your email has already been verified') } else if (!isCheckingAuth) { verifyEmail() } } }, [userId, verificationCode, isCheckingAuth, isEmailVerified, isVerifying, successMessage, errorMessage, verifyEmail]) // Render content based on current state const renderContent = () => { // Show loading state if (isCheckingAuth) { return ( <> Email Verification Checking verification status... ) } // Handle verification process if (userId && verificationCode) { return ( <> Email Verification {isVerifying ? 'Verifying your email address...' : 'Email Verification Status'} {isVerifying ? ( ) : successMessage ? ( ) : errorMessage ? ( ) : null} {successMessage && } ) } // Handle "check your email" state if (verificationEmailSent) { return ( <> Check Your Email We've sent you a verification email. Please check your inbox and click the link to verify your account. {successMessage && ( )} {errorMessage && ( )} {isEmailVerified && ( )} {isEmailVerified ? ( ) : (
Didn't receive the email?
)}
) } // Handle "send verification email" state return ( <> Email Verification {isLoggedIn ? isEmailVerified ? 'Your email is already verified' : 'Verify your email address to access all features' : 'You need to be logged in to verify your email' } {successMessage && ( )} {errorMessage && ( )} {isEmailVerified && ( )} {!isLoggedIn && ( Not Logged In You need to be logged in to verify your email )} {isLoggedIn ? ( isEmailVerified ? ( ) : ( ) ) : ( )} ) } return (
{renderContent()}
) }