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 { SignJWT, jwtVerify } from "jose";
import type { GlobalRole } from "@prisma/client";
@@ -64,7 +65,7 @@ export function sessionCookieOptions(remember: boolean) {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "strict" as const,
path: "/",
path: BASE_PATH || "/",
...(remember ? { maxAge: SESSION_DURATION_SECONDS } : {}),
};
}
+3 -2
View File
@@ -1,3 +1,4 @@
import { BASE_PATH, appUrl } from "@/lib/base-path";
import { SignJWT, jwtVerify } from "jose";
import type { AuthProvider } from "@prisma/client";
import { prisma } from "@/lib/db";
@@ -16,7 +17,7 @@ export const SOCIAL_PROVIDERS = ["google", "facebook"] as const;
export type SocialProvider = (typeof SOCIAL_PROVIDERS)[number];
export const SOCIAL_STATE_COOKIE = "pm_social_oauth";
export const SOCIAL_STATE_PATH = "/api/auth/oauth";
export const SOCIAL_STATE_PATH = `${BASE_PATH}/api/auth/oauth`;
export const SOCIAL_STATE_TTL_SECONDS = 10 * 60;
const DB_PROVIDER: Record<SocialProvider, AuthProvider> = { google: "GOOGLE", facebook: "FACEBOOK" };
@@ -75,7 +76,7 @@ export function safeRedirectPath(raw: string | null | undefined): string {
}
function redirectUri(provider: SocialProvider, origin: string): string {
return `${process.env.NEXT_PUBLIC_APP_URL || origin}/api/auth/oauth/${provider}/callback`;
return `${appUrl(origin)}/api/auth/oauth/${provider}/callback`;
}
// ---------- state ----------
+20
View File
@@ -0,0 +1,20 @@
/**
* The app can be served under a sub-path (e.g. https://loma.chat/pagiva).
* `NEXT_PUBLIC_BASE_PATH` is inlined at build time and also feeds
* `basePath` in next.config.ts — changing it requires a rebuild.
*
* Next.js prefixes <Link>, router.push/replace and redirect() itself; this
* helper is for everything it does not touch: fetch(), plain <a>/<img>,
* cookie paths and URLs built for third parties.
*/
export const BASE_PATH = (process.env.NEXT_PUBLIC_BASE_PATH ?? "").replace(/\/+$/, "");
/** Prefixes an absolute app path ("/api/...") with the base path. */
export function withBase(path: string): string {
return `${BASE_PATH}${path}`;
}
/** Public URL of the app root (origin + base path), no trailing slash. */
export function appUrl(origin: string): string {
return (process.env.NEXT_PUBLIC_APP_URL || `${origin}${BASE_PATH}`).replace(/\/+$/, "");
}
+2 -1
View File
@@ -1,3 +1,4 @@
import { appUrl } from "@/lib/base-path";
import { SignJWT, jwtVerify } from "jose";
import { GRAPH_API_VERSION, graphGet, UserFacingError } from "./client";
@@ -40,7 +41,7 @@ export function appAccessToken(): string | null {
}
export function oauthRedirectUri(origin: string): string {
return `${process.env.NEXT_PUBLIC_APP_URL || origin}/api/facebook/oauth/callback`;
return `${appUrl(origin)}/api/facebook/oauth/callback`;
}
export function oauthScopes(): string[] {
+2 -1
View File
@@ -1,3 +1,4 @@
import { withBase } from "@/lib/base-path";
import { NextResponse } from "next/server";
import { safeFacebookErrorMessage, UserFacingError, FacebookApiError } from "@/lib/facebook/client";
@@ -23,7 +24,7 @@ export function errorResponse(e: unknown, context: string): NextResponse {
* this page is same-site. `path` must already be a validated same-origin path.
*/
export function htmlRedirect(path: string): Response {
const safe = path.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
const safe = withBase(path).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
const html = `<!doctype html><meta charset="utf-8"><meta http-equiv="refresh" content="0;url=${safe}"><title>กำลังกลับสู่ระบบ…</title><p style="font-family:system-ui;padding:24px">กำลังกลับสู่ระบบ… <a href="${safe}">ไปต่อ</a></p>`;
return new Response(html, { headers: { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" } });
}