|
9 | 9 | * `/launch` route with `?iss=…&launch=…` populated, and our existing /launch |
10 | 10 | * handler runs the full PKCE dance. |
11 | 11 | */ |
| 12 | +// Resolve the public origin the SPA is served from, including any subpath |
| 13 | +// (e.g. /fhir-dsl/demo/). Used to derive redirect URIs at runtime so the |
| 14 | +// SMART flow works on localhost dev AND on https://awbx.github.io/fhir-dsl/demo |
| 15 | +// without env-var juggling. Only call from client-side code (uses `window`). |
| 16 | +function publicAppUrl(routePath: string): string { |
| 17 | + const base = import.meta.env.BASE_URL ?? "/"; |
| 18 | + const path = routePath.startsWith("/") ? routePath.slice(1) : routePath; |
| 19 | + return `${window.location.origin}${base}${path}`; |
| 20 | +} |
| 21 | + |
12 | 22 | export const SMART_CONFIG = { |
13 | 23 | /** Smart Health IT's launcher app — entry point for the demo's EHR-launch flow. */ |
14 | 24 | launcherUrl: |
15 | 25 | import.meta.env.VITE_SMART_LAUNCHER_URL ?? "https://launch.smarthealthit.org", |
16 | 26 | /** FHIR version the launcher should simulate (must match the sandbox endpoint). */ |
17 | 27 | launcherFhirVersion: import.meta.env.VITE_SMART_LAUNCHER_FHIR_VERSION ?? "r4", |
18 | 28 | clientId: import.meta.env.VITE_SMART_CLIENT_ID ?? "fhir-dsl-demo", |
19 | | - redirectUri: import.meta.env.VITE_SMART_REDIRECT_URI ?? "http://localhost:3000/callback", |
20 | | - /** EHR-launch entry point for our app — where the launcher hands us back. */ |
21 | | - appLaunchUrl: import.meta.env.VITE_SMART_APP_LAUNCH_URL ?? "http://localhost:3000/launch", |
22 | 29 | scope: |
23 | 30 | import.meta.env.VITE_SMART_SCOPE ?? |
24 | 31 | "launch openid fhirUser patient/*.read offline_access", |
25 | 32 | /** Default upstream FHIR for queries after login (R4). */ |
26 | 33 | fhirBaseUrl: import.meta.env.VITE_FHIR_BASE_URL ?? "https://hapi.fhir.org/baseR4", |
27 | 34 | } as const; |
28 | 35 |
|
| 36 | +/** OAuth redirect_uri — derived at call-site so it tracks the deployment URL. */ |
| 37 | +export function getRedirectUri(): string { |
| 38 | + return import.meta.env.VITE_SMART_REDIRECT_URI ?? publicAppUrl("callback"); |
| 39 | +} |
| 40 | + |
| 41 | +/** EHR-launch entry point — where the launcher hands us back. */ |
| 42 | +export function getAppLaunchUrl(): string { |
| 43 | + return import.meta.env.VITE_SMART_APP_LAUNCH_URL ?? publicAppUrl("launch"); |
| 44 | +} |
| 45 | + |
29 | 46 | export const TOKEN_STORE_KEY = `user:${SMART_CONFIG.clientId}`; |
30 | 47 |
|
31 | 48 | /** Build the URL the "Login with SMART" button should point at. */ |
32 | 49 | export function buildLauncherEntryUrl(): string { |
33 | 50 | const params = new URLSearchParams({ |
34 | | - launch_url: SMART_CONFIG.appLaunchUrl, |
| 51 | + launch_url: getAppLaunchUrl(), |
35 | 52 | fhir_version: SMART_CONFIG.launcherFhirVersion, |
36 | 53 | }); |
37 | 54 | return `${SMART_CONFIG.launcherUrl}/?${params.toString()}`; |
|
0 commit comments