Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 70x 26x 26x 2x 2x 2x 24x 24x 22x 8x 14x 14x 1x 23x 26x 70x 6x 6x 6x 4x 4x 4x 3x 3x 1x 4x 70x 26x 44x 36x 7x 7x 2x 8x 70x 1x 8x | import { useState, useEffect, type FormEvent } from "react";
import { Link, useSearch } from "@tanstack/react-router";
import { notify } from "../lib/notify";
import { authApi } from "../lib/api";
import { isValidEmail } from "../lib/validation";
import { API_BASE_URL } from "../lib/api/base";
import { Button } from "../components/ui/button";
import { Input } from "../components/ui/input";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "../components/ui/card";
import { LogoIcon } from "../components/ui/logo-icon";
export function VerifyEmailPage() {
const search = useSearch({ strict: false }) as { token?: string };
const token = search.token || "";
const [isLoading, setIsLoading] = useState(true);
const [success, setSuccess] = useState(false);
const [error, setError] = useState("");
// Resend verification state
const [resendEmail, setResendEmail] = useState("");
const [resendingEmail, setResendingEmail] = useState(false);
const [resendSuccess, setResendSuccess] = useState(false);
const [resendTouched, setResendTouched] = useState(false);
const resendEmailError = resendTouched && !resendEmail.trim()
? "Email is required"
: resendTouched && !isValidEmail(resendEmail.trim())
? "Enter a valid email"
: undefined;
useEffect(() => {
const verifyEmail = async () => {
if (!token) {
setError("Invalid verification link");
setIsLoading(false);
return;
}
try {
// Call Better Auth's verify email endpoint.
// Use redirect: "manual" because Better Auth may redirect after
// verification, which causes a cross-origin fetch failure.
const response = await fetch(
`${API_BASE_URL}/api/auth/verify-email?token=${encodeURIComponent(token)}`,
{
method: "GET",
credentials: "include",
redirect: "manual",
},
);
// A redirect (3xx) or 200 both indicate successful verification
if (response.ok || response.type === "opaqueredirect" || (response.status >= 300 && response.status < 400)) {
setSuccess(true);
} else {
const data = await response.json().catch(() => ({}));
setError(
data?.message || "Verification failed. The link may have expired.",
);
}
} catch (_err) {
setError("An error occurred during verification. Please try again.");
} finally {
setIsLoading(false);
}
};
verifyEmail();
}, [token]);
const handleResendVerification = async (e: FormEvent) => {
e.preventDefault();
setResendTouched(true);
if (!resendEmail.trim() || !isValidEmail(resendEmail.trim())) return;
setResendingEmail(true);
try {
await authApi.resendVerification(resendEmail.trim());
setResendSuccess(true);
notify.success("Verification email sent! Check your inbox.");
} catch {
notify.error("Failed to send verification email. Please try again.");
} finally {
setResendingEmail(false);
}
};
if (isLoading) {
return (
<div className="min-h-screen flex items-center justify-center bg-background-muted px-4">
<Card className="w-full max-w-md">
<CardHeader className="space-y-1">
<div className="flex items-center justify-center gap-2 mb-1">
<LogoIcon size={36} className="text-foreground-default" />
</div>
<CardTitle className="text-2xl text-center">Verifying...</CardTitle>
<CardDescription className="text-center">
Please wait while we verify your email address.
</CardDescription>
</CardHeader>
<CardContent className="flex justify-center">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600" />
</CardContent>
</Card>
</div>
);
}
if (error || !success) {
return (
<div className="min-h-screen flex items-center justify-center bg-background-muted px-4">
<Card className="w-full max-w-md">
<CardHeader className="space-y-1">
<div className="flex items-center justify-center gap-2 mb-1">
<LogoIcon size={36} className="text-foreground-default" />
</div>
<CardTitle className="text-2xl text-center">
Verification Failed
</CardTitle>
<CardDescription className="text-center text-red-600">
{error || "Unable to verify email"}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<p className="text-sm text-foreground-muted text-center">
The verification link may have expired or already been used.
Enter your email below to receive a new verification link.
</p>
<form onSubmit={handleResendVerification} className="space-y-3" noValidate>
<Input
type="email"
placeholder="you@example.com"
value={resendEmail}
onChange={(e) => {
setResendEmail(e.target.value);
setResendSuccess(false);
}}
onBlur={() => setResendTouched(true)}
error={resendEmailError}
autoComplete="email"
/>
<Button
type="submit"
className="w-full"
isLoading={resendingEmail}
disabled={resendSuccess}
>
{resendSuccess
? "Email sent"
: "Resend verification email"}
</Button>
</form>
{resendSuccess && (
<p className="text-sm text-green-600 text-center">
Check your inbox for the new verification link.
</p>
)}
<Link to="/login">
<Button variant="outline" className="w-full">
Back to Sign In
</Button>
</Link>
</CardContent>
</Card>
</div>
);
}
// Check if there's a pending invitation redirect from registration
const pendingRedirect = typeof window !== "undefined"
? localStorage.getItem("pendingInvitationRedirect")
: null;
// Clear it after reading so it doesn't persist across sessions
if (pendingRedirect) {
localStorage.removeItem("pendingInvitationRedirect");
}
return (
<div className="min-h-screen flex items-center justify-center bg-background-muted px-4">
<Card className="w-full max-w-md">
<CardHeader className="space-y-1">
<div className="flex items-center justify-center gap-2 mb-1">
<LogoIcon size={36} className="text-foreground-default" />
</div>
<CardTitle className="text-2xl text-center">
Email Verified!
</CardTitle>
<CardDescription className="text-center">
Your email has been successfully verified.
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<p className="text-sm text-foreground-muted text-center">
You can now sign in to your account and start using Interioring.
</p>
<Link to="/login" search={pendingRedirect ? { redirect: pendingRedirect } : undefined}>
<Button className="w-full">Sign In</Button>
</Link>
</CardContent>
</Card>
</div>
);
}
|