mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
Resolve OAuth callback URLs on the server for integration cards
The v1.38.0 notice that warns when the configured OAuth callback origin
differs from the origin the browser is on never rendered in the published
image, and the copyable callback address in the redirect guide showed a
bare path. `integrationCallbackUrl()` read `process.env.NEXT_PUBLIC_APP_URL`
inside a "use client" module. Next.js inlines NEXT_PUBLIC_* into client
bundles at build time, and the GHCR image is built without that variable
(only the version, build-sha and built-at args reach the build), so the
base compiled to an empty string, `new URL("/api/<provider>/callback")`
threw, and the notice returned null on every card for every self-hoster.
The value now comes from the server at request time. A new map
(`src/lib/integrations/callback-urls.ts`) calls each provider's own
redirect-URI function (the four private `getRedirectUri()` helpers are
exported under provider-specific names; Polar, Oura and Strava already
exported theirs), so the page shows exactly what the handshake registers,
explicit `*_REDIRECT_URI` override included. The settings route resolves
the map and passes it as a prop through `IntegrationsSection` and
`ConnectionsPanel` to every card. Fitbit and Google Health validate their
URI and throw on a bad configuration; that surfaces as `null`, the notice
skips the comparison and the guide falls back to the fixed path.
Tests: a map test proving the explicit override wins and the derived form
is used otherwise, a component test rendering the notice from a passed
value (mismatch renders, match does not), and a structural guard that
fails when any "use client" file under src/components/settings/integrations
reads NEXT_PUBLIC_APP_URL.
This commit is contained in:
1 parent
97b1603cb9
commit
05d3736d75
25 files changed
+516
-57
No files matched your search
@@ -40,25 +40,21 @@ export function integrationDocsHref(provider: IntegrationDocsProvider): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* The OAuth callback path every BYO-key provider registers against. Every
|
||||
* provider's `getRedirectUri()` (`src/lib/{provider}/client.ts`) derives the
|
||||
* same shape from `NEXT_PUBLIC_APP_URL` unless an operator overrides it with
|
||||
* an explicit `*_REDIRECT_URI` env var — the common case this card targets.
|
||||
* Shows a compact warning when the callback origin differs from the app.
|
||||
*
|
||||
* `callbackUrl` is the value the server registers as `redirect_uri`, resolved
|
||||
* per request by `getIntegrationCallbackUrls()` and passed down as a prop. It
|
||||
* must not be derived here: a `NEXT_PUBLIC_*` read in a client module is
|
||||
* inlined at build time, and the published image is built without it.
|
||||
* `null` means the provider cannot build a redirect URI from the current env,
|
||||
* so there is nothing to compare.
|
||||
*/
|
||||
export function integrationCallbackUrl(
|
||||
provider: IntegrationDocsProvider,
|
||||
): string {
|
||||
const base = process.env.NEXT_PUBLIC_APP_URL ?? "";
|
||||
return `${base}/api/${provider}/callback`;
|
||||
}
|
||||
|
||||
/** Shows a compact warning when the callback origin differs from the app. */
|
||||
export function CallbackMismatchNotice({
|
||||
provider,
|
||||
callbackUrl,
|
||||
}: {
|
||||
provider: string;
|
||||
callbackUrl: string;
|
||||
callbackUrl: string | null;
|
||||
}) {
|
||||
// Get the current origin of the app
|
||||
const { t } = useTranslations();
|
||||
@@ -69,6 +65,7 @@ export function CallbackMismatchNotice({
|
||||
);
|
||||
// Get the origin of the callback URL
|
||||
const callbackOrigin = (() => {
|
||||
if (!callbackUrl) return null;
|
||||
try {
|
||||
return new URL(callbackUrl).origin;
|
||||
} catch {
|
||||
@@ -103,12 +100,19 @@ export function CallbackMismatchNotice({
|
||||
export function IntegrationRedirectGuide({
|
||||
provider,
|
||||
providerLabel,
|
||||
callbackUrl,
|
||||
}: {
|
||||
provider: IntegrationDocsProvider;
|
||||
providerLabel: string;
|
||||
/**
|
||||
* The server-resolved callback URL (see `CallbackMismatchNotice`). When the
|
||||
* provider cannot build one from the current env, show the fixed path so
|
||||
* the step still names what the vendor form expects.
|
||||
*/
|
||||
callbackUrl: string | null;
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const callbackUrl = integrationCallbackUrl(provider);
|
||||
const shownCallbackUrl = callbackUrl ?? `/api/${provider}/callback`;
|
||||
return (
|
||||
<div
|
||||
className="bg-muted/40 border-border/60 space-y-1.5 rounded-md border p-3 text-xs"
|
||||
@@ -131,7 +135,7 @@ export function IntegrationRedirectGuide({
|
||||
className="bg-background text-foreground rounded px-1 py-0.5 font-mono break-all"
|
||||
data-testid={`${provider}-redirect-uri`}
|
||||
>
|
||||
{callbackUrl}
|
||||
{shownCallbackUrl}
|
||||
</code>
|
||||
</li>
|
||||
<li>{t("settings.integrationRedirectGuide.step3")}</li>
|
||||
|
||||
Reference in new issue
Block a user