2024-10-19 16:37:40 -04:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { useState } from "react";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import {
|
|
|
|
Card,
|
|
|
|
CardContent,
|
|
|
|
CardDescription,
|
|
|
|
CardHeader,
|
|
|
|
CardTitle,
|
|
|
|
} from "@/components/ui/card";
|
|
|
|
import {
|
|
|
|
Form,
|
|
|
|
FormControl,
|
|
|
|
FormDescription,
|
|
|
|
FormField,
|
|
|
|
FormItem,
|
|
|
|
FormLabel,
|
|
|
|
FormMessage,
|
|
|
|
} from "@/components/ui/form";
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
import {
|
|
|
|
InputOTP,
|
|
|
|
InputOTPGroup,
|
|
|
|
InputOTPSlot,
|
|
|
|
} from "@/components/ui/input-otp";
|
|
|
|
import { AxiosResponse } from "axios";
|
|
|
|
import { VerifyEmailResponse } from "@server/routers/auth";
|
|
|
|
import { Loader2 } from "lucide-react";
|
|
|
|
import { Alert, AlertDescription } from "../../../components/ui/alert";
|
2025-02-10 21:35:06 -05:00
|
|
|
import { toast } from "@app/hooks/useToast";
|
2024-10-19 16:37:40 -04:00
|
|
|
import { useRouter } from "next/navigation";
|
2025-01-01 21:41:31 -05:00
|
|
|
import { formatAxiosError } from "@app/lib/api";;
|
|
|
|
import { createApiClient } from "@app/lib/api";
|
2024-12-12 22:46:58 -05:00
|
|
|
import { useEnvContext } from "@app/hooks/useEnvContext";
|
2025-01-09 23:21:57 -05:00
|
|
|
import { cleanRedirect } from "@app/lib/cleanRedirect";
|
2024-10-19 16:37:40 -04:00
|
|
|
|
|
|
|
const FormSchema = z.object({
|
|
|
|
email: z.string().email({ message: "Invalid email address" }),
|
|
|
|
pin: z.string().min(8, {
|
|
|
|
message: "Your verification code must be 8 characters.",
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type VerifyEmailFormProps = {
|
|
|
|
email: string;
|
|
|
|
redirect?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function VerifyEmailForm({
|
|
|
|
email,
|
|
|
|
redirect,
|
|
|
|
}: VerifyEmailFormProps) {
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const [successMessage, setSuccessMessage] = useState<string | null>(null);
|
|
|
|
const [isResending, setIsResending] = useState(false);
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
|
2024-12-12 22:46:58 -05:00
|
|
|
const api = createApiClient(useEnvContext());
|
|
|
|
|
2024-10-19 16:37:40 -04:00
|
|
|
const form = useForm<z.infer<typeof FormSchema>>({
|
|
|
|
resolver: zodResolver(FormSchema),
|
|
|
|
defaultValues: {
|
|
|
|
email: email,
|
|
|
|
pin: "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
async function onSubmit(data: z.infer<typeof FormSchema>) {
|
|
|
|
setIsSubmitting(true);
|
|
|
|
|
|
|
|
const res = await api
|
|
|
|
.post<AxiosResponse<VerifyEmailResponse>>("/auth/verify-email", {
|
|
|
|
code: data.pin,
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
2024-11-13 20:08:05 -05:00
|
|
|
setError(formatAxiosError(e, "An error occurred"));
|
2024-10-19 16:37:40 -04:00
|
|
|
console.error("Failed to verify email:", e);
|
2024-11-28 00:11:13 -05:00
|
|
|
setIsSubmitting(false);
|
2024-10-19 16:37:40 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
if (res && res.data?.data?.valid) {
|
|
|
|
setError(null);
|
|
|
|
setSuccessMessage(
|
2024-11-13 20:08:05 -05:00
|
|
|
"Email successfully verified! Redirecting you..."
|
2024-10-19 16:37:40 -04:00
|
|
|
);
|
|
|
|
setTimeout(() => {
|
|
|
|
if (redirect) {
|
2025-01-09 23:21:57 -05:00
|
|
|
const safe = cleanRedirect(redirect);
|
|
|
|
router.push(safe);
|
2024-10-19 16:37:40 -04:00
|
|
|
} else {
|
|
|
|
router.push("/");
|
|
|
|
}
|
|
|
|
setIsSubmitting(false);
|
|
|
|
}, 1500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleResendCode() {
|
|
|
|
setIsResending(true);
|
|
|
|
|
|
|
|
const res = await api.post("/auth/verify-email/request").catch((e) => {
|
2024-11-13 20:08:05 -05:00
|
|
|
setError(formatAxiosError(e, "An error occurred"));
|
2024-10-19 16:37:40 -04:00
|
|
|
console.error("Failed to resend verification code:", e);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res) {
|
|
|
|
setError(null);
|
|
|
|
toast({
|
|
|
|
variant: "default",
|
|
|
|
title: "Verification code resent",
|
|
|
|
description:
|
|
|
|
"We've resent a verification code to your email address. Please check your inbox.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsResending(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2024-11-23 20:08:56 -05:00
|
|
|
<Card className="w-full max-w-md">
|
2024-10-19 16:37:40 -04:00
|
|
|
<CardHeader>
|
2024-11-28 00:11:13 -05:00
|
|
|
<CardTitle>Verify Email</CardTitle>
|
2024-10-19 16:37:40 -04:00
|
|
|
<CardDescription>
|
|
|
|
Enter the verification code sent to your email address.
|
|
|
|
</CardDescription>
|
|
|
|
</CardHeader>
|
|
|
|
<CardContent>
|
|
|
|
<Form {...form}>
|
|
|
|
<form
|
|
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
2024-12-22 17:09:22 -05:00
|
|
|
className="space-y-4"
|
2024-10-19 16:37:40 -04:00
|
|
|
>
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="email"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Email</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<Input
|
|
|
|
{...field}
|
|
|
|
disabled
|
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="pin"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem>
|
|
|
|
<FormLabel>Verification Code</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<div className="flex justify-center">
|
|
|
|
<InputOTP
|
|
|
|
maxLength={8}
|
|
|
|
{...field}
|
|
|
|
>
|
|
|
|
<InputOTPGroup className="flex">
|
|
|
|
<InputOTPSlot
|
|
|
|
index={0}
|
|
|
|
/>
|
|
|
|
<InputOTPSlot
|
|
|
|
index={1}
|
|
|
|
/>
|
|
|
|
<InputOTPSlot
|
|
|
|
index={2}
|
|
|
|
/>
|
|
|
|
<InputOTPSlot
|
|
|
|
index={3}
|
|
|
|
/>
|
|
|
|
<InputOTPSlot
|
|
|
|
index={4}
|
|
|
|
/>
|
|
|
|
<InputOTPSlot
|
|
|
|
index={5}
|
|
|
|
/>
|
|
|
|
<InputOTPSlot
|
|
|
|
index={6}
|
|
|
|
/>
|
|
|
|
<InputOTPSlot
|
|
|
|
index={7}
|
|
|
|
/>
|
|
|
|
</InputOTPGroup>
|
|
|
|
</InputOTP>
|
|
|
|
</div>
|
|
|
|
</FormControl>
|
2025-02-26 21:24:35 -05:00
|
|
|
<FormMessage />
|
2024-10-19 16:37:40 -04:00
|
|
|
<FormDescription>
|
|
|
|
We sent a verification code to your
|
2025-03-01 23:03:42 -05:00
|
|
|
email address.
|
2024-10-19 16:37:40 -04:00
|
|
|
</FormDescription>
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
<Alert variant="destructive">
|
|
|
|
<AlertDescription>{error}</AlertDescription>
|
|
|
|
</Alert>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{successMessage && (
|
|
|
|
<Alert variant="success">
|
|
|
|
<AlertDescription>
|
|
|
|
{successMessage}
|
|
|
|
</AlertDescription>
|
|
|
|
</Alert>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
className="w-full"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
>
|
|
|
|
{isSubmitting && (
|
|
|
|
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
|
|
|
)}
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
|
2024-11-28 00:11:13 -05:00
|
|
|
<div className="text-center text-muted-foreground mt-2">
|
2024-10-19 16:37:40 -04:00
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
variant="link"
|
|
|
|
onClick={handleResendCode}
|
|
|
|
disabled={isResending}
|
|
|
|
>
|
|
|
|
{isResending
|
|
|
|
? "Resending..."
|
|
|
|
: "Didn't receive a code? Click here to resend"}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|