"use client"; import * as React from "react"; import { useSearchParams } from "next/navigation"; import { zodResolver } from "@hookform/resolvers/zod"; import { signIn } from "next-auth/react"; import { useTranslations } from "next-intl"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import useSWR from "swr"; import * as z from "zod"; import { siteConfig } from "@/config/site"; import { cn, fetcher } from "@/lib/utils"; import { userAuthSchema } from "@/lib/validations/auth"; import { buttonVariants } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Icons } from "@/components/shared/icons"; import { Skeleton } from "../ui/skeleton"; interface UserAuthFormProps extends React.HTMLAttributes { type?: string; } type FormData = z.infer; export function UserAuthForm({ className, type, ...props }: UserAuthFormProps) { const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(userAuthSchema), }); const [isLoading, setIsLoading] = React.useState(false); const [isGoogleLoading, setIsGoogleLoading] = React.useState(false); const [isGithubLoading, setIsGithubLoading] = React.useState(false); const [isLinuxDoLoading, setIsLinuxDoLoading] = React.useState(false); const searchParams = useSearchParams(); const t = useTranslations("Auth"); async function onSubmit(data: FormData) { setIsLoading(true); const signInResult = await signIn("resend", { email: data.email.toLowerCase(), redirect: false, callbackUrl: searchParams?.get("from") || "/dashboard", }); setIsLoading(false); if (!signInResult?.ok) { return toast.error("Something went wrong.", { description: "Your sign in request failed. Please try again.", }); } return toast.success("Check your email", { description: "We sent you a login link. Be sure to check your spam too.", }); } const { data: loginMethod, isLoading: isLoadingMethod } = useSWR< Record >("/api/feature", fetcher, { revalidateOnFocus: false, }); const rendeSeparator = () => { return (
{t("Or continue with")}
); }; if (isLoadingMethod || !loginMethod) { return (
{rendeSeparator()}
); } return (
{loginMethod["google"] && ( )} {loginMethod["github"] && ( )} {loginMethod["linuxdo"] && ( )} {(loginMethod["google"] || loginMethod["github"] || loginMethod["linuxdo"]) && loginMethod["resend"] && rendeSeparator()} {loginMethod["resend"] && (
{errors?.email && (

{errors.email.message}

)}
)}
); }