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