Files
PAGIVA/src/components/LoginForm.tsx
T
Loma Bot Ops 4a673ebb28 Support sub-path deploy via NEXT_PUBLIC_BASE_PATH (loma.chat/pagiva)
Adds basePath config, withBase/appUrl helpers, base-path-aware cookies,
OAuth redirects, manifest, service worker and cron runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:48:42 +07:00

146 lines
7.5 KiB
TypeScript

"use client";
import { withBase } from "@/lib/base-path";
import { useState } from "react";
import { useRouter, useSearchParams } from "next/navigation";
import { Icon } from "@/components/Icon";
type Provider = "google" | "facebook";
const PROVIDER_LABEL: Record<Provider, string> = { google: "Google", facebook: "Facebook" };
/** Same-origin path only — "//host" and "/\host" would leave the site. */
function safeFrom(raw: string | null): string {
return raw && raw.startsWith("/") && !raw.startsWith("//") && !raw.startsWith("/\\") ? raw : "/";
}
// Social callbacks redirect with a code, never free text, so the URL can't be
// used to put arbitrary wording on the login page.
function socialErrorMessage(params: URLSearchParams): string | null {
const code = params.get("error");
if (!code) return null;
const p = params.get("provider");
const label = p === "google" || p === "facebook" ? PROVIDER_LABEL[p] : "บัญชีนี้";
const rawEmail = params.get("email") ?? "";
const email = /^[^\s@<>]{1,64}@[^\s@<>]{1,190}$/.test(rawEmail) ? rawEmail : null;
switch (code) {
case "no_account":
return `ยังไม่มีบัญชีของ ${email ?? "อีเมลนี้"} ในระบบ — ให้ผู้ดูแลระบบเพิ่มผู้ใช้ด้วยอีเมลนี้ก่อน แล้วกดเข้าสู่ระบบด้วย ${label} อีกครั้ง`;
case "no_email":
return `บัญชี ${label} นี้ไม่มีอีเมลที่ยืนยันแล้ว — เข้าสู่ระบบด้วยอีเมลและรหัสผ่านแทน`;
case "linked_other":
return `บัญชีนี้ผูกกับ ${label} อีกบัญชีไว้แล้ว — เลือกบัญชี ${label} เดิม หรือใช้อีเมลและรหัสผ่าน`;
case "inactive":
return "บัญชีนี้ถูกปิดใช้งาน ติดต่อผู้ดูแลระบบ";
case "cancelled":
return "ยกเลิกการเข้าสู่ระบบแล้ว";
case "expired":
return "ลิงก์เข้าสู่ระบบหมดอายุ กดอีกครั้ง";
case "not_configured":
return "ยังไม่ได้ตั้งค่าการเข้าสู่ระบบด้วยบัญชีนี้";
default:
return `เข้าสู่ระบบด้วย ${label} ไม่สำเร็จ ลองอีกครั้ง`;
}
}
function GoogleMark() {
return (
<svg viewBox="0 0 48 48" width={20} height={20} aria-hidden="true">
<path fill="#EA4335" d="M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z" />
<path fill="#4285F4" d="M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z" />
<path fill="#FBBC05" d="M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z" />
<path fill="#34A853" d="M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z" />
</svg>
);
}
function FacebookMark() {
return (
<svg viewBox="0 0 24 24" width={20} height={20} aria-hidden="true">
<path fill="#1877F2" d="M24 12.073C24 5.405 18.627 0 12 0S0 5.405 0 12.073C0 18.1 4.388 23.094 10.125 24v-8.437H7.078v-3.49h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.49h-2.796V24C19.612 23.094 24 18.1 24 12.073z" />
</svg>
);
}
export function LoginForm({ providers }: { providers: Provider[] }) {
const router = useRouter();
const params = useSearchParams();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [remember, setRemember] = useState(true);
const [error, setError] = useState<string | null>(() => socialErrorMessage(params));
const [busy, setBusy] = useState(false);
const from = safeFrom(params.get("from"));
async function submit(e: React.FormEvent) {
e.preventDefault();
setBusy(true);
setError(null);
const res = await fetch(withBase("/api/auth/login"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password, remember }),
});
const body = await res.json().catch(() => ({}));
setBusy(false);
if (!res.ok) {
setError(body.error ?? "เข้าสู่ระบบไม่สำเร็จ");
return;
}
router.replace(body.mustChangePassword ? "/change-password" : from);
router.refresh();
}
function socialHref(p: Provider): string {
const q = new URLSearchParams({ from, remember: remember ? "1" : "0" });
return withBase(`/api/auth/oauth/${p}/start?${q}`);
}
return (
<form onSubmit={submit} className="login-form">
<div className="mb-3">
<h1 className="text-[26px] font-bold">ยินดีต้อนรับกลับ</h1>
<p className="text-ink-3 text-[13px] mt-2">เข้าสู่ระบบเพื่อดูแลเพจของคุณต่อ</p>
</div>
{providers.length > 0 && (
<>
<div className="flex flex-col gap-2.5">
{providers.includes("google") && (
<a className="btn btn-ghost btn-lg" href={socialHref("google")}>
<GoogleMark /> เข้าสู่ระบบด้วย Google
</a>
)}
{providers.includes("facebook") && (
<a className="btn btn-ghost btn-lg" href={socialHref("facebook")}>
<FacebookMark /> เข้าสู่ระบบด้วย Facebook
</a>
)}
</div>
<div className="login-divider">หรือใช้อีเมล</div>
</>
)}
<label className="field">
<span>อีเมล</span>
<input className="input" type="email" autoComplete="email" placeholder="name@company.com" required value={email} onChange={(e) => setEmail(e.target.value)} />
</label>
<label className="field">
<span>รหัสผ่าน</span>
<input className="input" type="password" autoComplete="current-password" placeholder="กรอกรหัสผ่านของคุณ" required value={password} onChange={(e) => setPassword(e.target.value)} />
</label>
<label className="flex items-center gap-2 text-[13px] text-ink-2 py-1">
<input type="checkbox" checked={remember} onChange={(e) => setRemember(e.target.checked)} />
จดจำการเข้าสู่ระบบ
</label>
{error && (
<p className="notice notice-red" role="alert">
{error}
</p>
)}
<button className="btn btn-primary btn-lg" disabled={busy}>
{busy ? "กำลังเข้าสู่ระบบ…" : <>เข้าสู่ระบบ <Icon name="right" size={17} /></>}
</button>
<p className="flex justify-center gap-1.5 items-center text-[11px] text-ink-3 mt-3"><Icon name="shield" size={13} /> พื้นที่ทำงานสำหรับทีมของคุณ</p>
</form>
);
}