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>
This commit is contained in:
Loma Bot Ops
2026-11-22 16:48:42 +07:00
parent 9d5a029fa5
commit 4a673ebb28
38 changed files with 130 additions and 57 deletions
+2 -1
View File
@@ -1,3 +1,4 @@
import { BASE_PATH } from "@/lib/base-path";
import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { requireUser } from "@/lib/auth/user";
@@ -39,7 +40,7 @@ export async function POST(req: Request) {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "strict",
path: "/",
path: BASE_PATH || "/",
maxAge: SESSION_DURATION_SECONDS,
});
return res;
+2 -1
View File
@@ -1,8 +1,9 @@
import { BASE_PATH } from "@/lib/base-path";
import { NextResponse } from "next/server";
import { COOKIE_NAME } from "@/lib/auth/session";
export async function POST() {
const res = NextResponse.json({ ok: true });
res.cookies.delete(COOKIE_NAME);
res.cookies.delete({ name: COOKIE_NAME, path: BASE_PATH || "/" });
return res;
}
@@ -1,3 +1,4 @@
import { appUrl } from "@/lib/base-path";
import { NextResponse } from "next/server";
import {
authorizeUrl,
@@ -19,7 +20,7 @@ export async function GET(req: Request, ctx: RouteContext<"/api/auth/oauth/[prov
const url = new URL(req.url);
const provider = parseProvider(raw);
if (!provider || !enabledProviders().includes(provider)) {
return NextResponse.redirect(new URL("/login?error=not_configured", url.origin));
return NextResponse.redirect(`${appUrl(url.origin)}/login?error=not_configured`);
}
const { nonce, cookie } = await createSocialState(provider, {
+2 -1
View File
@@ -1,3 +1,4 @@
import { BASE_PATH } from "@/lib/base-path";
import { cookies } from "next/headers";
import { prisma } from "@/lib/db";
import { encryptToken } from "@/lib/crypto/tokenBox";
@@ -22,7 +23,7 @@ export async function GET(req: Request) {
const url = new URL(req.url);
const jar = await cookies();
const stateCookie = jar.get(OAUTH_STATE_COOKIE)?.value;
jar.delete({ name: OAUTH_STATE_COOKIE, path: "/api/facebook/oauth" });
jar.delete({ name: OAUTH_STATE_COOKIE, path: `${BASE_PATH}/api/facebook/oauth` });
const userId = await verifyOAuthState(stateCookie, url.searchParams.get("state"));
if (!userId) return hop(`/pages?fb_error=${encodeURIComponent("ลิงก์เชื่อมหมดอายุหรือไม่ถูกต้อง ลองกดเชื่อมใหม่")}`);
+5 -3
View File
@@ -1,3 +1,4 @@
import { BASE_PATH, appUrl } from "@/lib/base-path";
import { NextResponse } from "next/server";
import { getCurrentUser, isSuperadmin } from "@/lib/auth/user";
import { authorizeUrl, createOAuthState, OAUTH_STATE_COOKIE } from "@/lib/facebook/oauth";
@@ -6,8 +7,9 @@ import { safeFacebookErrorMessage } from "@/lib/facebook/client";
/** SUPERADMIN only: redirects to the Facebook consent dialog. */
export async function GET(req: Request) {
const origin = new URL(req.url).origin;
const root = appUrl(origin);
const user = await getCurrentUser();
if (!user || !isSuperadmin(user)) return NextResponse.redirect(new URL("/pages?fb_error=forbidden", origin));
if (!user || !isSuperadmin(user)) return NextResponse.redirect(`${root}/pages?fb_error=forbidden`);
try {
const { nonce, cookie } = await createOAuthState(user.id);
@@ -17,12 +19,12 @@ export async function GET(req: Request) {
secure: process.env.NODE_ENV === "production",
// Lax: must survive the top-level redirect back from facebook.com.
sameSite: "lax",
path: "/api/facebook/oauth",
path: `${BASE_PATH}/api/facebook/oauth`,
maxAge: 600,
});
return res;
} catch (e) {
const msg = safeFacebookErrorMessage(e, "fb:oauth:start");
return NextResponse.redirect(new URL(`/pages?fb_error=${encodeURIComponent(msg)}`, origin));
return NextResponse.redirect(`${root}/pages?fb_error=${encodeURIComponent(msg)}`);
}
}
+2 -1
View File
@@ -1,3 +1,4 @@
import { appUrl } from "@/lib/base-path";
import { NextResponse } from "next/server";
import { putBlob } from "@/lib/storage/blob";
import { requirePageAction } from "@/lib/auth/user";
@@ -26,7 +27,7 @@ export async function POST(req: Request) {
try {
const ext = file.type === "image/png" ? "png" : "jpg";
const baseUrl = process.env.NEXT_PUBLIC_APP_URL || new URL(req.url).origin;
const baseUrl = appUrl(new URL(req.url).origin);
const blob = await putBlob(`posts/${pageId}/image.${ext}`, file, { contentType: file.type }, baseUrl);
return NextResponse.json({ url: blob.url });
} catch (e) {
+3 -2
View File
@@ -1,4 +1,5 @@
"use client";
import { withBase } from "@/lib/base-path";
import { useState } from "react";
import { useRouter } from "next/navigation";
@@ -17,7 +18,7 @@ export default function ChangePasswordPage() {
if (newPassword !== confirm) return setError("รหัสผ่านใหม่ทั้งสองช่องไม่ตรงกัน");
setBusy(true);
setError(null);
const res = await fetch("/api/auth/change-password", {
const res = await fetch(withBase("/api/auth/change-password"), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ currentPassword, newPassword }),
@@ -30,7 +31,7 @@ export default function ChangePasswordPage() {
}
async function logout() {
await fetch("/api/auth/logout", { method: "POST" });
await fetch(withBase("/api/auth/logout"), { method: "POST" });
router.replace("/login");
}
+2 -1
View File
@@ -1,3 +1,4 @@
import { withBase } from "@/lib/base-path";
import type { Metadata, Viewport } from "next";
import localFont from "next/font/local";
import { ServiceWorker } from "@/components/ServiceWorker";
@@ -17,7 +18,7 @@ export const metadata: Metadata = {
description: "วางแผน อนุมัติ และเผยแพร่โพสต์ของเพจ Facebook และ Instagram ในพื้นที่ทำงานเดียว",
applicationName: "จัดการเพจ",
appleWebApp: { capable: true, title: "จัดการเพจ", statusBarStyle: "default" },
icons: { icon: "/icons/192", apple: "/icons/apple" },
icons: { icon: withBase("/icons/192"), apple: withBase("/icons/apple") },
formatDetection: { telephone: false },
};
+10 -9
View File
@@ -1,27 +1,28 @@
import { withBase } from "@/lib/base-path";
import type { MetadataRoute } from "next";
export default function manifest(): MetadataRoute.Manifest {
return {
id: "/",
id: withBase("/"),
name: "ระบบจัดการเพจ",
short_name: "จัดการเพจ",
description: "โพสต์ อนุมัติ ตอบแชท และดู insights ของเพจ Facebook และ Instagram",
start_url: "/",
scope: "/",
start_url: withBase("/"),
scope: withBase("/"),
display: "standalone",
orientation: "portrait",
background_color: "#eef8ff",
theme_color: "#e0f2fe",
lang: "th",
icons: [
{ src: "/icons/192", sizes: "192x192", type: "image/png", purpose: "any" },
{ src: "/icons/512", sizes: "512x512", type: "image/png", purpose: "any" },
{ src: "/icons/maskable-512", sizes: "512x512", type: "image/png", purpose: "maskable" },
{ src: withBase("/icons/192"), sizes: "192x192", type: "image/png", purpose: "any" },
{ src: withBase("/icons/512"), sizes: "512x512", type: "image/png", purpose: "any" },
{ src: withBase("/icons/maskable-512"), sizes: "512x512", type: "image/png", purpose: "maskable" },
],
shortcuts: [
{ name: "สร้างโพสต์", short_name: "สร้างโพสต์", url: "/posts/new" },
{ name: "Inbox", url: "/inbox" },
{ name: "ปฏิทิน", url: "/calendar" },
{ name: "สร้างโพสต์", short_name: "สร้างโพสต์", url: withBase("/posts/new") },
{ name: "Inbox", url: withBase("/inbox") },
{ name: "ปฏิทิน", url: withBase("/calendar") },
],
};
}