Files
co-lite/solvway-admin-portal/app/lib/server/apiRoute.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

46 lines
2.4 KiB
TypeScript

import "server-only";
import { NextResponse } from "next/server";
import type { ApiResult } from "@/app/lib/types";
import { isSessionReplaced, markReplacedOn } from "@/app/lib/server/signedInElsewhere";
/** Build the error response for a failed ApiResult.
* 401 is reserved for expired/absent session (the client redirects to /login);
* a genuine upstream 4xx (e.g. 404 for a missing record, 409 for a conflict)
* is forwarded as-is so the page can branch on the real status instead of
* sniffing the message text; any other failure (5xx, unknown) falls back to
* 400 so the page can show the message inline. */
export function errorResponse(r: Extract<ApiResult<unknown>, { ok: false }>): NextResponse {
const status = r.expired ? 401 : r.status && r.status >= 400 && r.status < 500 ? r.status : 400;
const body: {
error: string;
code?: string;
errors?: string[];
details?: Record<string, string | number | boolean>;
} = { error: r.error };
if (r.code) body.code = r.code;
if (r.errors) body.errors = r.errors;
if (r.details) body.details = r.details;
const res = NextResponse.json(body, { status });
// one-session-per-account: a newer sign-in ended this session; end it here and tell the login page why.
return isSessionReplaced(r) ? markReplacedOn(res) : res;
}
export function unauthorized(message = "Not authenticated"): NextResponse {
return NextResponse.json({ error: message }, { status: 401 });
}
export function badRequest(message: string, extra?: Record<string, unknown>): NextResponse {
return NextResponse.json({ error: message, ...(extra ?? {}) }, { status: 400 });
}
/** A `linkKind: "URL"` reference must be either a site-relative path or an absolute http(s) URL.
* It later renders verbatim as an anchor href on the anonymous storefront, so anything else
* (e.g. `javascript:...`) is an XSS/open-redirect surface and must be rejected at the boundary.
* A leading `//` (or `/\`) is protocol-relative — browsers resolve it against the current
* scheme onto whatever host follows, i.e. it is effectively an absolute off-site URL wearing a
* single leading slash — so a "starts with /" check alone is not sufficient and must exclude it. */
export function isSafeUrlLinkReference(value: string): boolean {
if (/^https?:\/\//i.test(value)) return true;
return value.startsWith("/") && !value.startsWith("//") && !value.startsWith("/\\");
}