Use the fhir-client.js in a Multi-page Architecture

Introduction

This sample will show using a FHIR Client in a multi-state architecture.

Saving the FHIR Client's State

After the FHIR Client has completed the authorization flow (after redirect), save the state in the windows session.

Saving FHIR Client State
    FHIR.oauth2.ready(function (fhirClient) {
        // save the fhirClient state for subsequent page calls
        window.sessionStorage.smartServiceUrl = fhirClient.server.serviceUrl;
        window.sessionStorage.smartPatientId = fhirClient.patient.id;
        window.sessionStorage.smartAuthToken = fhirClient.server.auth.token;
        window.sessionStorage.smartAuthType = fhirClient.server.auth.type;
    });

Instantiating a FHIR Client from Saved State

Subsequent web pages can instantiate an instance of the FHIR Client.

Instantiating FHIR Client from Saved State
	var fhirClient = FHIR.client({
		serviceUrl: window.sessionStorage.smartServiceUrl,
		patientId: window.sessionStorage.smartPatientId,
		auth: {
			token: window.sessionStorage.smartAuthToken,
			type: window.sessionStorage.smartAuthType
		}
	});

	var patient = fhirClient.patient;