|
| 1 | +import FHIR from "fhirclient"; |
| 2 | +import { queryNeedPatientBanner, queryPatientIdKey } from "@consts"; |
| 3 | +import { fetchEnvData, getEnv } from "@util"; |
| 4 | + |
| 5 | +const fetchContextJson = async (authURL) => { |
| 6 | + if (!authURL) { |
| 7 | + // default, if no auth url provided |
| 8 | + return { |
| 9 | + clientId: "hpai_sof_client", |
| 10 | + scope: "profile roles email patient/*.read", |
| 11 | + // default to not show patient banner, can be overridden |
| 12 | + // cannot seem to override this when testing against SMART healthIT launcher though |
| 13 | + need_patient_banner: false, |
| 14 | + }; |
| 15 | + } |
| 16 | + const response = await fetch(authURL, { |
| 17 | + // include cookies in request |
| 18 | + credentials: "include", |
| 19 | + }).catch((e) => { |
| 20 | + console.log(e); |
| 21 | + throw new Error("Error retrieving context json via auth url. See console for detail."); |
| 22 | + }); |
| 23 | + |
| 24 | + if (!response.ok) { |
| 25 | + console.log(response.status, response.statusText); |
| 26 | + throw new Error(`Error launch application: Server returned status ${response.status.toString()}`); |
| 27 | + } |
| 28 | + |
| 29 | + const contextJson = await response.json().catch((e) => { |
| 30 | + console.log(e); |
| 31 | + throw new Error("Context json parsing error. See console for detail."); |
| 32 | + }); |
| 33 | + |
| 34 | + return contextJson; |
| 35 | +}; |
| 36 | + |
| 37 | +fetchEnvData().then((results) => { |
| 38 | + console.log("environment variables ", results); |
| 39 | + const backendURL = getEnv("REACT_APP_CONF_API_URL"); |
| 40 | + const authURL = backendURL ? `${backendURL}/auth/auth-info` : ""; |
| 41 | + const urlParams = new URLSearchParams(window.location.search); |
| 42 | + const patientId = urlParams.get("patient"); |
| 43 | + console.log("patient id from url query string: ", patientId); |
| 44 | + const needPatientBanner = urlParams.get("need_patient_banner"); |
| 45 | + console.log("need_patient_banner from url query string: ", needPatientBanner); |
| 46 | + console.log("Auth url ", authURL); |
| 47 | + |
| 48 | + fetchContextJson(authURL) |
| 49 | + .then((json) => { |
| 50 | + if (patientId) { |
| 51 | + json.patientId = patientId; |
| 52 | + sessionStorage.setItem(queryPatientIdKey, patientId); |
| 53 | + } |
| 54 | + |
| 55 | + if (needPatientBanner !== null) { |
| 56 | + json.need_patient_banner = needPatientBanner; |
| 57 | + sessionStorage.setItem(queryNeedPatientBanner, needPatientBanner); |
| 58 | + } |
| 59 | + |
| 60 | + // allow client id to be configurable |
| 61 | + const envClientId = getEnv("REACT_APP_CLIENT_ID"); |
| 62 | + if (envClientId) json.clientId = envClientId; |
| 63 | + |
| 64 | + // allow auth scopes to be updated via environment variable |
| 65 | + // see https://build.fhir.org/ig/HL7/smart-app-launch/scopes-and-launch-context.html |
| 66 | + const envAuthScopes = getEnv("REACT_APP_AUTH_SCOPES"); |
| 67 | + if (envAuthScopes) json.scope = envAuthScopes; |
| 68 | + |
| 69 | + sessionStorage.setItem("launchContextJson", JSON.stringify(json)); |
| 70 | + |
| 71 | + console.log("launch context json ", json); |
| 72 | + FHIR.oauth2 |
| 73 | + .authorize(json) |
| 74 | + .catch((e) => { |
| 75 | + console.log("FHIR auth error ", e); |
| 76 | + }); |
| 77 | + }) |
| 78 | + .catch((error) => console.log(error)); |
| 79 | +}); |
0 commit comments