Spaces:
Running
Running
Download web/src/components/shell/auth-form.tsx from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 6.42 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/shell/auth-form.tsx
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/components/shell/auth-form.tsx
-
curl -L -o auth-form.tsx https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/shell/auth-form.tsx
6.42 kB
| "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<string | null>(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 ( | |
| <div className="grid min-h-dvh lg:grid-cols-[1fr_minmax(0,32rem)]"> | |
| <section className="relative hidden flex-col justify-between overflow-hidden bg-sidebar p-10 lg:flex"> | |
| <div className="flex items-center gap-2 font-semibold"> | |
| <span className="grid size-7 place-items-center rounded-md bg-primary text-primary-foreground" aria-hidden> | |
| <svg viewBox="0 0 24 24" className="size-4" fill="none" stroke="currentColor" strokeWidth={2.2}> | |
| <path d="M4 7l4 10 4-7 4 7 4-10" strokeLinecap="round" strokeLinejoin="round" /> | |
| </svg> | |
| </span> | |
| {t("app.name")} | |
| </div> | |
| <div className="max-w-md space-y-4"> | |
| <ShieldCheckIcon className="size-8 text-primary" aria-hidden /> | |
| <p className="text-2xl leading-snug font-semibold tracking-tight">{t("app.tagline")}</p> | |
| <p className="text-muted-foreground">{t("auth.pitch")}</p> | |
| </div> | |
| <p className="text-xs text-muted-foreground">© WorkPilot</p> | |
| </section> | |
| <section className="flex flex-col px-6 py-8 sm:px-10"> | |
| <div className="flex justify-end"> | |
| <Button | |
| variant="ghost" | |
| size="sm" | |
| onClick={() => setLocale(locale === "en" ? "ur" : "en")} | |
| className={locale === "en" ? "font-urdu" : ""} | |
| > | |
| {locale === "en" ? "اردو" : "English"} | |
| </Button> | |
| </div> | |
| <div className="mx-auto flex w-full max-w-sm flex-1 flex-col justify-center"> | |
| <h1 className="mb-1 text-xl font-semibold tracking-tight"> | |
| {mode === "login" ? t("auth.loginTitle") : t("auth.signupTitle")} | |
| </h1> | |
| <p className="mb-6 text-sm text-muted-foreground"> | |
| {mode === "login" ? t("auth.noAccount") : t("auth.haveAccount")}{" "} | |
| <Link | |
| href={mode === "login" ? "/signup" : "/login"} | |
| className="font-medium text-primary underline-offset-4 hover:underline" | |
| > | |
| {mode === "login" ? t("auth.signup") : t("auth.login")} | |
| </Link> | |
| </p> | |
| <form onSubmit={submit} className="space-y-4" noValidate={false}> | |
| {mode === "signup" && ( | |
| <div className="space-y-2"> | |
| <Label htmlFor="business">{t("auth.businessName")}</Label> | |
| <Input | |
| id="business" | |
| required | |
| minLength={2} | |
| maxLength={200} | |
| autoComplete="organization" | |
| value={business} | |
| onChange={(e) => setBusiness(e.target.value)} | |
| /> | |
| </div> | |
| )} | |
| <div className="space-y-2"> | |
| <Label htmlFor="email">{t("auth.email")}</Label> | |
| <Input | |
| id="email" | |
| type="email" | |
| dir="ltr" | |
| required | |
| autoComplete="email" | |
| value={email} | |
| onChange={(e) => setEmail(e.target.value)} | |
| /> | |
| </div> | |
| <div className="space-y-2"> | |
| <Label htmlFor="password">{t("auth.password")}</Label> | |
| <Input | |
| id="password" | |
| type="password" | |
| dir="ltr" | |
| required | |
| minLength={mode === "signup" ? 10 : undefined} | |
| autoComplete={mode === "login" ? "current-password" : "new-password"} | |
| value={password} | |
| onChange={(e) => setPassword(e.target.value)} | |
| aria-describedby={mode === "signup" ? "pw-hint" : undefined} | |
| /> | |
| {mode === "signup" && ( | |
| <p id="pw-hint" className="text-xs text-muted-foreground"> | |
| {t("auth.passwordHint")} | |
| </p> | |
| )} | |
| </div> | |
| {error && ( | |
| <p role="alert" className="text-sm text-destructive"> | |
| {error} | |
| </p> | |
| )} | |
| <Button type="submit" size="lg" className="w-full" disabled={busy}> | |
| {busy && <Loader2Icon className="animate-spin" aria-hidden />} | |
| {mode === "login" ? t("auth.login") : t("auth.signup")} | |
| </Button> | |
| </form> | |
| </div> | |
| </section> | |
| </div> | |
| ); | |
| } | |