|
|
@@ -47,6 +47,17 @@ function resolveApiBaseUrlValue(raw: string | undefined): string {
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
+/** PM2 / server.js 常用对外误显端口;登录跳转一律不写进地址栏(本机回环除外)。 */
|
|
|
+const DEFAULT_NODE_PUBLIC_PORT = "4000";
|
|
|
+
|
|
|
+function isLoopbackHostname(hostname: string): boolean {
|
|
|
+ return (
|
|
|
+ hostname === "localhost" ||
|
|
|
+ hostname === "127.0.0.1" ||
|
|
|
+ hostname === "[::1]"
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
/** 本地 / 局域网访问时保留当前 origin(含端口),避免误跳到 :80/:443 上无服务。 */
|
|
|
function shouldPreserveCurrentOriginPort(hostname: string): boolean {
|
|
|
if (
|
|
|
@@ -74,17 +85,23 @@ function shouldPreserveCurrentOriginPort(hostname: string): boolean {
|
|
|
|
|
|
/**
|
|
|
* 登录页跳转地址(仅浏览器)。
|
|
|
- * - `window.location.assign("/auth/login")` 会保留 `:4000` 等当前端口。
|
|
|
+ * - 当前端口为 **4000** 且非本机回环时,强制用「协议 + 主机名(无端口)」拼 `/auth/login`,去掉地址栏里的 `:4000`。
|
|
|
* - 若已配置 `NEXT_PUBLIC_SITE_URL` 且当前 host 与站点一致,优先用该 canonical origin。
|
|
|
- * - 否则对公网域名用「当前协议 + 主机名(不含端口)」拼路径,这样即使构建时未打入
|
|
|
- * SITE_URL,从 `jinclab.com:4000` 也会跳到 `https://jinclab.com/auth/login`(由中间件补 `/zh`)。
|
|
|
- * - localhost / 局域网 IP 仍用相对路径,保留端口。
|
|
|
+ * - localhost / 局域网 IP 且非上述 4000 规则:仍用相对路径,保留端口。
|
|
|
*/
|
|
|
export function resolveAuthLoginHref(): string {
|
|
|
const path = "/auth/login";
|
|
|
if (typeof window === "undefined") return path;
|
|
|
|
|
|
- const { protocol, hostname } = window.location;
|
|
|
+ const { protocol, hostname, port } = window.location;
|
|
|
+
|
|
|
+ if (port === DEFAULT_NODE_PUBLIC_PORT && !isLoopbackHostname(hostname)) {
|
|
|
+ try {
|
|
|
+ return new URL(path, `${protocol}//${hostname}/`).href;
|
|
|
+ } catch {
|
|
|
+ /* fall through */
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
const site = process.env.NEXT_PUBLIC_SITE_URL?.trim().replace(/\/$/, "");
|
|
|
if (site) {
|