|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef } from "react"; |
| 4 | +import { useRouter, useSearchParams } from "next/navigation"; |
| 5 | +import { toast } from "sonner"; |
| 6 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; |
| 7 | +import { Button } from "@/components/ui/button"; |
| 8 | +import { useServerAction } from "zsa-react"; |
| 9 | +import { verifyEmailAction } from "./verify-email.action"; |
| 10 | +import { verifyEmailSchema } from "@/schemas/verify-email.schema"; |
| 11 | +import { Spinner } from "@/components/ui/spinner"; |
| 12 | + |
| 13 | +export default function VerifyEmailClientComponent() { |
| 14 | + const router = useRouter(); |
| 15 | + const searchParams = useSearchParams(); |
| 16 | + const token = searchParams.get("token"); |
| 17 | + const hasCalledVerification = useRef(false); |
| 18 | + |
| 19 | + const { execute: handleVerification, isPending, error } = useServerAction(verifyEmailAction, { |
| 20 | + onError: ({ err }) => { |
| 21 | + toast.dismiss(); |
| 22 | + toast.error(err.message || "Failed to verify email"); |
| 23 | + }, |
| 24 | + onStart: () => { |
| 25 | + toast.loading("Verifying your email..."); |
| 26 | + }, |
| 27 | + onSuccess: () => { |
| 28 | + toast.dismiss(); |
| 29 | + toast.success("Email verified successfully"); |
| 30 | + |
| 31 | + router.refresh(); |
| 32 | + |
| 33 | + setTimeout(() => { |
| 34 | + router.push("/dashboard"); |
| 35 | + }, 500); |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + if (token && !hasCalledVerification.current) { |
| 41 | + const result = verifyEmailSchema.safeParse({ token }); |
| 42 | + if (result.success) { |
| 43 | + hasCalledVerification.current = true; |
| 44 | + handleVerification(result.data); |
| 45 | + } else { |
| 46 | + toast.error("Invalid verification token"); |
| 47 | + router.push("/sign-in"); |
| 48 | + } |
| 49 | + } |
| 50 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 51 | + }, [token]); |
| 52 | + |
| 53 | + if (isPending) { |
| 54 | + return ( |
| 55 | + <div className="container mx-auto px-4 flex items-center justify-center min-h-screen"> |
| 56 | + <Card className="w-full max-w-md"> |
| 57 | + <CardHeader className="text-center"> |
| 58 | + <div className="flex flex-col items-center space-y-4"> |
| 59 | + <Spinner size="large" /> |
| 60 | + <CardTitle>Verifying Email</CardTitle> |
| 61 | + <CardDescription> |
| 62 | + Please wait while we verify your email address... |
| 63 | + </CardDescription> |
| 64 | + </div> |
| 65 | + </CardHeader> |
| 66 | + </Card> |
| 67 | + </div> |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + if (error) { |
| 72 | + return ( |
| 73 | + <div className="container mx-auto px-4 flex items-center justify-center min-h-screen"> |
| 74 | + <Card className="w-full max-w-md"> |
| 75 | + <CardHeader> |
| 76 | + <CardTitle>Verification failed</CardTitle> |
| 77 | + <CardDescription> |
| 78 | + {error?.message || "Failed to verify email"} |
| 79 | + </CardDescription> |
| 80 | + </CardHeader> |
| 81 | + <CardContent> |
| 82 | + <Button |
| 83 | + variant="outline" |
| 84 | + className="w-full" |
| 85 | + onClick={() => router.push("/sign-in")} |
| 86 | + > |
| 87 | + Back to sign in |
| 88 | + </Button> |
| 89 | + </CardContent> |
| 90 | + </Card> |
| 91 | + </div> |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + if (!token) { |
| 96 | + return ( |
| 97 | + <div className="container mx-auto px-4 flex items-center justify-center min-h-screen"> |
| 98 | + <Card className="w-full max-w-md"> |
| 99 | + <CardHeader> |
| 100 | + <CardTitle>Invalid verification link</CardTitle> |
| 101 | + <CardDescription> |
| 102 | + The verification link is invalid. Please request a new verification email. |
| 103 | + </CardDescription> |
| 104 | + </CardHeader> |
| 105 | + <CardContent> |
| 106 | + <Button |
| 107 | + variant="outline" |
| 108 | + className="w-full" |
| 109 | + onClick={() => router.push("/sign-in")} |
| 110 | + > |
| 111 | + Back to sign in |
| 112 | + </Button> |
| 113 | + </CardContent> |
| 114 | + </Card> |
| 115 | + </div> |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + return null; |
| 120 | +} |
0 commit comments