1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
"use client";
import { useEffect, useState } from "react";
import { useRouter, useSearchParams } from "next/navigation";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { api } from "@/lib/trpc";
import { CheckCircle, Loader2, XCircle } from "lucide-react";
export default function VerifyEmailPage() {
const searchParams = useSearchParams();
const router = useRouter();
const [status, setStatus] = useState<"loading" | "success" | "error">(
"loading",
);
const [message, setMessage] = useState("");
const token = searchParams.get("token");
const email = searchParams.get("email");
const verifyEmailMutation = api.users.verifyEmail.useMutation({
onSuccess: () => {
setStatus("success");
setMessage(
"Your email has been successfully verified! You can now sign in.",
);
},
onError: (error) => {
setStatus("error");
setMessage(
error.message ||
"Failed to verify email. The link may be invalid or expired.",
);
},
});
const resendEmailMutation = api.users.resendVerificationEmail.useMutation({
onSuccess: () => {
setMessage(
"A new verification email has been sent to your email address.",
);
},
onError: (error) => {
setMessage(error.message || "Failed to resend verification email.");
},
});
useEffect(() => {
if (token && email) {
verifyEmailMutation.mutate({ token, email });
} else {
setStatus("error");
setMessage("Invalid verification link. Missing token or email.");
}
}, [token, email]);
const handleResendEmail = () => {
if (email) {
resendEmailMutation.mutate({ email });
}
};
const handleSignIn = () => {
router.push("/signin");
};
return (
<div className="flex min-h-screen items-center justify-center bg-background px-4 py-12 sm:px-6 lg:px-8">
<Card className="w-full max-w-md">
<CardHeader className="text-center">
<CardTitle className="text-2xl font-bold">
Email Verification
</CardTitle>
<CardDescription>
{status === "loading" && "Verifying your email address..."}
{status === "success" && "Email verified successfully!"}
{status === "error" && "Verification failed"}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{status === "loading" && (
<div className="flex items-center justify-center">
<Loader2 className="h-8 w-8 animate-spin text-blue-600" />
</div>
)}
{status === "success" && (
<>
<div className="flex items-center justify-center">
<CheckCircle className="h-12 w-12 text-green-600" />
</div>
<Alert>
<AlertDescription className="text-center">
{message}
</AlertDescription>
</Alert>
<Button onClick={handleSignIn} className="w-full">
Sign In
</Button>
</>
)}
{status === "error" && (
<>
<div className="flex items-center justify-center">
<XCircle className="h-12 w-12 text-red-600" />
</div>
<Alert variant="destructive">
<AlertDescription className="text-center">
{message}
</AlertDescription>
</Alert>
{email && (
<div className="space-y-2">
<Button
onClick={handleResendEmail}
variant="outline"
className="w-full"
disabled={resendEmailMutation.isPending}
>
{resendEmailMutation.isPending ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Sending...
</>
) : (
"Resend Verification Email"
)}
</Button>
<Button
onClick={handleSignIn}
variant="ghost"
className="w-full"
>
Back to Sign In
</Button>
</div>
)}
</>
)}
</CardContent>
</Card>
</div>
);
}
|