fix: use public origin for installation demo shares

This commit is contained in:
WuYongJie 2026-07-10 14:23:20 +08:00
parent be49070962
commit d0af5d85dc
3 changed files with 46 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import { NextRequest } from 'next/server'
import { AppError } from '@/server/core/errors'
import { handleRequest, jsonError, jsonSuccess } from '@/server/core/response'
import { installationDemoShareService } from '@/server/services'
import { buildInstallationDemoShareUrl } from './share-url'
export async function POST(request: NextRequest) {
return handleRequest(
@ -14,7 +15,7 @@ export async function POST(request: NextRequest) {
{
share: {
token: snapshot.token,
url: `${request.nextUrl.origin}/installation-demo-share/${snapshot.token}`,
url: buildInstallationDemoShareUrl(snapshot.token, request.nextUrl.origin),
expiresAt: snapshot.expiresAt,
snapshot,
},

View File

@ -0,0 +1,38 @@
import { buildInstallationDemoShareUrl } from './share-url'
const originalPublicAppUrl = process.env.NEXT_PUBLIC_APP_URL
const originalNextAuthUrl = process.env.NEXTAUTH_URL
describe('buildInstallationDemoShareUrl', () => {
afterAll(() => {
if (originalPublicAppUrl === undefined) {
delete process.env.NEXT_PUBLIC_APP_URL
} else {
process.env.NEXT_PUBLIC_APP_URL = originalPublicAppUrl
}
if (originalNextAuthUrl === undefined) {
delete process.env.NEXTAUTH_URL
} else {
process.env.NEXTAUTH_URL = originalNextAuthUrl
}
})
it('uses the configured public origin instead of the internal listener address', () => {
process.env.NEXT_PUBLIC_APP_URL = 'https://cp.jiansun.vip/'
delete process.env.NEXTAUTH_URL
expect(
buildInstallationDemoShareUrl('share-token-1', 'https://0.0.0.0:3001'),
).toBe('https://cp.jiansun.vip/installation-demo-share/share-token-1')
})
it('uses the request origin for local development when no public origin is configured', () => {
delete process.env.NEXT_PUBLIC_APP_URL
delete process.env.NEXTAUTH_URL
expect(
buildInstallationDemoShareUrl('share-token-2', 'http://localhost:3001'),
).toBe('http://localhost:3001/installation-demo-share/share-token-2')
})
})

View File

@ -0,0 +1,6 @@
export function buildInstallationDemoShareUrl(token: string, requestOrigin: string) {
const publicOrigin =
process.env.NEXT_PUBLIC_APP_URL?.trim() || process.env.NEXTAUTH_URL?.trim() || requestOrigin
return new URL(`/installation-demo-share/${token}`, publicOrigin).toString()
}