import { useState } from "react"; import { Link, redirect, useLoaderData } from "react-router"; import { authClient } from "~/lib/auth-client"; import { Button } from "~/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "~/components/ui/card"; import type { Route } from "./+types/check-email"; export function meta(): Route.MetaDescriptors { return [{ title: "Check Your Email - Brackt" }]; } export function safeRedirectTo(value: string | null): string { if (!value) return "/"; return value.startsWith("/") && !value.startsWith("//") ? value : "/"; } export async function loader(args: Route.LoaderArgs) { const url = new URL(args.request.url); const email = url.searchParams.get("email"); if (!email) return redirect("/register"); return { email, redirectTo: safeRedirectTo(url.searchParams.get("redirectTo")), }; } type ResendState = "idle" | "sending" | "sent" | "error"; export default function CheckEmailPage() { const { email, redirectTo } = useLoaderData(); const [resendState, setResendState] = useState("idle"); const [resendError, setResendError] = useState(null); async function handleResend() { setResendState("sending"); setResendError(null); const { error } = await authClient.sendVerificationEmail({ email, callbackURL: redirectTo, }); if (error) { setResendState("error"); setResendError(error.message ?? "Could not resend. Please try again."); } else { setResendState("sent"); } } return (
Check your email We sent a verification link to {email}

Click the link in the email to verify your account and get started. If you don't see it, check your spam folder.

{resendState === "sent" && (

Email sent! Check your inbox.

)} {resendState === "error" && resendError && (

{resendError}

)}

Wrong account?{" "} Sign in

); }