fix(app): auth routes outside admin layout (/signin), image upload verified end-to-end

This commit is contained in:
BANK committed 2026-10-27 22:44:07 +07:00
1 parent 5fdd78acc6
commit eb0081036e
7 files changed
+58 -67

No files matched your search

+1 -1
View File
@@ -90,7 +90,7 @@ export function requireAdmin(request: Request): AdminSession {
if (!session) {
const url = new URL(request.url);
const next = `${url.pathname}${url.search}`;
throw redirect(`/admin/login?next=${encodeURIComponent(next)}`);
throw redirect(`/signin?next=${encodeURIComponent(next)}`);
}
return session;
}
+2 -2
View File
@@ -146,7 +146,7 @@ export default function AdminRoute() {
return;
}
setAuthState("denied");
window.location.replace("/admin/login?next=%2Fadmin");
window.location.replace("/signin?next=%2Fadmin");
})
.catch(() => {
if (alive) setAuthState("denied");
@@ -335,7 +335,7 @@ export default function AdminRoute() {
<IconDeviceFloppy size={16} />
{saving ? "กำลังบันทึก…" : "บันทึก"}
</button>
<Form method="post" action="/admin/logout">
<Form method="post" action="/signout">
<button
type="submit"
className="flex items-center gap-2 rounded-lg border border-[#303030] px-3 py-2 text-sm text-[#B3B3B3] transition hover:border-[#A92626] hover:text-white"
@@ -1,59 +1,32 @@
import { Form, Link, useActionData, useNavigation } from "react-router";
import { IconArrowLeft, IconLock } from "@tabler/icons-react";
import { Link, useSearchParams } from "react-router";
import {
createSessionCookie,
verifyCredentials,
} from "@/lib/auth.server";
import { redirect } from "react-router";
/** ปลายทางหลังล็อกอิน — รับเฉพาะ path ในเว็บนี้ (กัน open redirect) */
function safeNext(raw: string | null): string {
if (!raw) return "/admin";
return raw.startsWith("/") && !raw.startsWith("//") ? raw : "/admin";
}
export async function action({ request }: { request: Request }) {
const form = await request.formData();
const username = String(form.get("username") ?? "").trim();
const password = String(form.get("password") ?? "");
const next = safeNext(String(form.get("next") ?? ""));
if (!verifyCredentials(username, password)) {
return {
ok: false,
error: "ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง",
username,
next,
};
}
throw redirect(next, {
headers: { "Set-Cookie": createSessionCookie() },
});
}
/**
* หน้าล็อกอินหลังบ้าน — /signin
* ------------------------------------------------------------
* ★ อยู่นอก namespace admin.* เพราะ admin.tsx เป็น layout ของ admin.* ทั้งหมด
* (ถ้าอยู่ใต้ admin. จะโดน gate ของ layout บังและไม่เคย render)
* ★ ฟอร์ม HTML ธรรมดา POST ไป /signinsubmit (action-only, ไม่มี JS ก็ล็อกอินได้)
* ★ ไฟล์นี้ไม่ import โมดูลฝั่ง server เลย → hydrate ปกติ
*/
const inputClass =
"w-full rounded-lg border border-[#303030] bg-[#161616] px-4 py-3 text-sm text-white outline-none transition focus:border-[#A92626]";
export function meta() {
return [{ title: "เข้าสู่ระบบ — AlterFlixHub" }];
}
const inputClass =
"w-full rounded-lg border border-[#303030] bg-[#161616] px-4 py-3 text-sm text-white outline-none transition focus:border-[#A92626]";
export default function LoginRoute() {
const result = useActionData() as
| { ok: false; error: string; username: string; next: string }
| undefined;
const navigation = useNavigation();
const busy = navigation.state === "submitting";
export default function SignInRoute() {
const [params] = useSearchParams();
const next = params.get("next") ?? "/admin";
const failed = params.get("error") === "1";
return (
<div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-[#0D0D0D] px-4 text-white">
<div className="relative flex min-h-screen items-center justify-center overflow-hidden bg-[#0D0D0D] px-4 py-10 text-white">
<div
className="pointer-events-none absolute left-1/2 top-1/3 size-[420px] -translate-x-1/2 -translate-y-1/2 rounded-full opacity-70 blur-[200px]"
style={{ background: "#8F0808" }}
/>
<div className="relative w-full max-w-[420px]">
<Link
to="/"
@@ -70,22 +43,19 @@ export default function LoginRoute() {
<h1 className="mt-2 flex items-center gap-2 text-lg font-semibold">
<IconLock size={18} /> เข้าสู่ระบบหลังบ้าน
</h1>
<p className="text-xs text-[#888]">
<p className="text-xs text-[#999]">
เฉพาะผู้ดูแลเว็บ — ใช้แก้ไขข้อมูลโรงหนังและสถานะการแสดงผล
</p>
</div>
<Form method="post" className="mt-6 flex flex-col gap-4">
<input type="hidden" name="next" value={result?.next ?? "/admin"} />
<form method="post" action="/signinsubmit" className="mt-6 flex flex-col gap-4">
<input type="hidden" name="next" value={next} />
<label className="flex flex-col gap-1">
<span className="text-xs font-medium text-[#B3B3B3]">
ชื่อผู้ใช้
</span>
<span className="text-xs font-medium text-[#B3B3B3]">ชื่อผู้ใช้</span>
<input
name="username"
autoComplete="username"
defaultValue={result?.username ?? ""}
className={inputClass}
placeholder="ชื่อผู้ใช้"
required
@@ -104,23 +74,22 @@ export default function LoginRoute() {
/>
</label>
{result?.error ? (
{failed ? (
<p className="rounded-lg bg-[#3a1113] px-3 py-2 text-sm text-[#ff9a9a]">
{result.error}
ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง
</p>
) : null}
<button
type="submit"
disabled={busy}
className="mt-2 h-11 rounded-lg bg-[#8F0808] text-sm font-medium transition hover:bg-[#a30a0a] disabled:opacity-50"
className="mt-2 h-11 rounded-lg bg-[#8F0808] text-sm font-medium transition hover:bg-[#a30a0a]"
>
{busy ? "กำลังตรวจสอบ…" : "เข้าสู่ระบบ"}
เข้าสู่ระบบ
</button>
</Form>
</form>
</div>
<p className="mt-4 text-center text-[11px] text-[#555]">
<p className="mt-4 text-center text-[11px] text-[#777]">
ล็อกอินค้างไว้ 12 ชั่วโมง · ออกจากระบบได้จากหน้าหลังบ้าน
</p>
</div>
+26
View File
@@ -0,0 +1,26 @@
import { redirect } from "react-router";
import { createSessionCookie, verifyCredentials } from "@/lib/auth.server";
/** รับฟอร์มล็อกอิน — /signinsubmit (action-only, ไม่ถูกส่งลง client bundle) */
function safeNext(raw: string | null): string {
if (!raw) return "/admin";
return raw.startsWith("/") && !raw.startsWith("//") ? raw : "/admin";
}
export async function action({ request }: { request: Request }) {
const form = await request.formData();
const username = String(form.get("username") ?? "").trim();
const password = String(form.get("password") ?? "");
const next = safeNext(String(form.get("next") ?? ""));
if (!verifyCredentials(username, password)) {
throw redirect(`/signin?error=1&next=${encodeURIComponent(next)}`);
}
throw redirect(next, { headers: { "Set-Cookie": createSessionCookie() } });
}
export async function loader() {
throw redirect("/signin");
}
@@ -2,7 +2,7 @@ import { redirect } from "react-router";
import { clearSessionCookie } from "@/lib/auth.server";
/** ออกจากระบบ — ล้าง cookie แล้วกลับหน้าแรก */
/** ออกจากระบบ — /signout (action-only) */
export async function action() {
throw redirect("/", { headers: { "Set-Cookie": clearSessionCookie() } });
}
@@ -10,7 +10,3 @@ export async function action() {
export async function loader() {
throw redirect("/");
}
export default function LogoutRoute() {
return null;
}
+2 -2
View File
@@ -1,5 +1,5 @@
{
"updated_at": "2026-10-27T15:19:41.647Z",
"updated_at": "2026-10-27T15:43:35.189Z",
"theaters": [
{
"slug": "house-cinema-samyan",
@@ -95,7 +95,7 @@
}
],
"hidden": false,
"image": null
"image": "/images/theaters/house-cinema-samyan-1793115815167.png"
},
{
"slug": "major-hollywood",
Binary file not shown.

After

Width:  |  Height:  |  Size: 350 KiB