"use client"; import { useState } from "react"; import { signIn } from "next-auth/react"; import toast, { Toaster } from "react-hot-toast"; import { isEmail } from "@/lib/utils"; import { Github, Google, LoadingDots } from "../shared/icons"; import { usePathname } from "next/navigation"; export default function EmailButton() { const pathname = usePathname(); const [email, setEmail] = useState(""); const [loading, setLoading] = useState(false); const [isSendSuccess, setIsSendSuccess] = useState(false); const [signInGithubClicked, setSignInGithubClicked] = useState(false); const [signInGoogleClicked, setSignInGoogleClicked] = useState(false); const handleSubmit = async () => { if (email === "") { toast("Empty email", { icon: "😅", }); return; } if (!isEmail(email)) { toast("Invalid email format", { icon: "😅", }); return; } setIsSendSuccess(false); setLoading(true); const sign_req = await signIn("email", { email: email, callbackUrl: pathname, redirect: false, }); setTimeout(() => { setLoading(false); }, 20000); if (sign_req?.ok) { setLoading(false); setIsSendSuccess(true); } else if (sign_req?.error) { toast("Sending failed, please try again", { icon: "😅", }); setLoading(false); setIsSendSuccess(false); } }; const handleKeydown = (key: string) => { if (key === "Enter") { handleSubmit(); } }; return ( <> setEmail(e.target.value)} onKeyDown={(e) => handleKeydown(e.key)} />
or
); }