Spaces:
Running

File size: 6,416 Bytes
03608f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"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>
  );
}