"use client"; import { Loader2Icon, ShieldCheckIcon } from "lucide-react"; import Link from "next/link"; import { useRouter, useSearchParams } from "next/navigation"; import { useEffect, useState, type FormEvent } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useAuth } from "@/lib/auth"; import { useI18n } from "@/lib/i18n"; import { SessionError } from "@/lib/session"; function safeNext(next: string | null): string { // Only same-app paths: never an absolute URL (open redirect). return next && next.startsWith("/") && !next.startsWith("//") ? next : "/inbox"; } export function AuthForm({ mode }: { mode: "login" | "signup" }) { const { t, locale, setLocale } = useI18n(); const { status, login, signup } = useAuth(); const router = useRouter(); const params = useSearchParams(); const next = safeNext(params.get("next")); const [business, setBusiness] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); useEffect(() => { if (status === "authenticated") router.replace(next); }, [status, router, next]); const submit = async (e: FormEvent) => { e.preventDefault(); setBusy(true); setError(null); try { if (mode === "login") await login(email, password); else await signup(business, email, password); router.replace(next); } catch (err) { if (err instanceof SessionError && err.status === 401) setError(t("auth.invalid")); else if (err instanceof SessionError && err.status === 502) setError(t("errors.network")); else setError(err instanceof Error ? err.message : t("common.error")); } finally { setBusy(false); } }; return (
{t("app.name")}

{t("app.tagline")}

{t("auth.pitch")}

© WorkPilot

{mode === "login" ? t("auth.loginTitle") : t("auth.signupTitle")}

{mode === "login" ? t("auth.noAccount") : t("auth.haveAccount")}{" "} {mode === "login" ? t("auth.signup") : t("auth.login")}

{mode === "signup" && (
setBusiness(e.target.value)} />
)}
setEmail(e.target.value)} />
setPassword(e.target.value)} aria-describedby={mode === "signup" ? "pw-hint" : undefined} /> {mode === "signup" && (

{t("auth.passwordHint")}

)}
{error && (

{error}

)}
); }