"use client"; import { useEffect, useState } from "react"; import { Logo } from "@/components/Logo"; import { createClient } from "@/utils/supabase/client"; // Sign in with a code from your email. The same email also has a link, which still works (/auth/callback). // Rate limits and wrong-browser links were the common trouble with links alone; a code works in any tab. export default function LoginPage() { const [email, setEmail] = useState(""); const [code, setCode] = useState(""); const [state, setState] = useState<"idle" | "sending" | "sent" | "checking" | "error">("idle"); const [sent, setSent] = useState(false); const [message, setMessage] = useState(""); const [firstRun, setFirstRun] = useState(null); const [devLogin, setDevLogin] = useState(false); useEffect(() => { if (new URLSearchParams(window.location.search).get("error") === "link") { setState("error"); setMessage("That sign-in link didn't work. Each link works once, for about an hour, and only in the browser you asked for it from. Send a new email and open its link here."); } fetch("/api/setup/first-run").then((r) => r.json()).then((b: { supabase?: string; devLogin?: boolean }) => { setFirstRun(b.supabase ?? null); setDevLogin(!!b.devLogin); }).catch(() => {}); }, []); async function send(e?: React.FormEvent) { e?.preventDefault(); setState("sending"); const { error } = await createClient().auth.signInWithOtp({ email, options: { emailRedirectTo: `${window.location.origin}/auth/callback` }, }); if (error) { setState("error"); setMessage(/rate limit|too many/i.test(error.message) ? "Too many sign-in emails for now. Supabase's free email sends a few an hour. Wait a bit, or use the link from the last email." : error.message); } else { setSent(true); setCode(""); setState("sent"); } } async function verify(e: React.FormEvent) { e.preventDefault(); setState("checking"); const { error } = await createClient().auth.verifyOtp({ email, token: code.replace(/\s/g, ""), type: "email" }); if (error) { setState("error"); setCode(""); // ready for the right one, not mixed in with the wrong one setMessage(/expired|invalid/i.test(error.message) ? "That code didn't work. Codes work once, for about an hour. Check you typed the newest one, or send a new email." : error.message); } else window.location.assign("/"); // a full load: the page cache may still hold "/" from before sign-in (a redirect back here) } return (
{firstRun && (

One step before your first sign-in

Tell Supabase this site's address, or the email will link to the wrong place.

  1. Open URL Configuration in Supabase.
  2. Set Site URL to {window.location.origin} and press Save.
)}

{sent ? `We sent an email to ${email}.` : "Sign in with your email."}

{sent ? ( <>

Open the sign-in link in that email, in this browser. Or, if the email has a code, type it here.

setCode(e.target.value.replace(/[^\d\s]/g, ""))} placeholder="Code" aria-label="Code from the email" className="input w-full tabular tracking-[0.2em] text-center text-lg" /> {state === "error" &&

{message}

}
) : ( <> setEmail(e.target.value)} placeholder="you@example.com" className="input w-full" /> {state === "error" &&

{message}

} {devLogin && ( )} )}
); }