fix: coerce register override away from page route (#410) (#415)

Co-authored-by: root <root@global-hub.svc.plus>
This commit is contained in:
shenlan 2025-10-06 10:09:10 +08:00 committed by GitHub
parent 2615464fa3
commit 59198a19d0
2 changed files with 69 additions and 1 deletions

View File

@ -0,0 +1,23 @@
# Registration 404 troubleshooting
If the registration form fails with a `404` in the network tab even though the
Account Service is reachable, double-check the URL the browser is posting to.
The frontend page at `/register` renders the form, but it does not accept
`POST` requests itself. API calls must go through the Next.js route exposed at
`/api/auth/register`, which proxies the request to the Account Service.
Recent builds automatically coerce a misconfigured
`NEXT_PUBLIC_REGISTER_URL` that points to `/register` to the correct
`/api/auth/register` endpoint. If you are debugging an older build or the error
persists, unset the override so that the default endpoint is used or point the
variable to the API handler directly (for example,
`https://svc.plus/api/auth/register`).
Related source files:
- `ui/homepage/app/register/RegisterContent.tsx` reads
`NEXT_PUBLIC_REGISTER_URL` and submits the form to that URL.
- `ui/homepage/app/api/auth/register/route.ts` handles the
`/api/auth/register` requests and forwards them to the account service.
- `account/api/api.go` exposes the `POST /api/auth/register` handler inside the
account service.

View File

@ -16,6 +16,51 @@ import { WeChatIcon } from '../components/icons/WeChatIcon'
type AlertState = { type: 'error' | 'success'; message: string }
function normalizePathname(pathname: string): string {
return pathname.replace(/\/+$/, '').toLowerCase()
}
function coerceRegisterUrlOverride(rawValue: string | undefined | null, accountServiceBaseUrl: string): string {
const fallbackUrl = `${accountServiceBaseUrl}/api/auth/register`
if (!rawValue) {
return fallbackUrl
}
const trimmed = rawValue.trim()
if (!trimmed) {
return fallbackUrl
}
const rewritePathname = (pathname: string) => {
const normalized = normalizePathname(pathname)
if (normalized === '/register' || normalized === 'register') {
return '/api/auth/register'
}
return undefined
}
try {
const parsed = new URL(trimmed)
const rewritten = rewritePathname(parsed.pathname)
if (rewritten) {
parsed.pathname = rewritten
return parsed.toString()
}
return parsed.toString()
} catch (error) {
try {
const parsed = new URL(trimmed, 'http://localhost')
const rewritten = rewritePathname(parsed.pathname)
if (rewritten) {
return `${rewritten}${parsed.search}${parsed.hash}`
}
} catch (relativeError) {
console.warn('Failed to parse register URL override', relativeError)
}
return trimmed
}
}
export default function RegisterContent() {
const { language } = useLanguage()
const t = translations[language].auth.register
@ -26,7 +71,7 @@ export default function RegisterContent() {
const accountServiceBaseUrl = getAccountServiceBaseUrl()
const githubAuthUrl = process.env.NEXT_PUBLIC_GITHUB_AUTH_URL || '/api/auth/github'
const wechatAuthUrl = process.env.NEXT_PUBLIC_WECHAT_AUTH_URL || '/api/auth/wechat'
const registerUrl = process.env.NEXT_PUBLIC_REGISTER_URL || `${accountServiceBaseUrl}/api/auth/register`
const registerUrl = coerceRegisterUrlOverride(process.env.NEXT_PUBLIC_REGISTER_URL, accountServiceBaseUrl)
const isSocialAuthVisible = false
const registerUrlRef = useRef(registerUrl)