Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Introduction

Excerpt

This sample will demonstrate how to retrieve the current user within an app, using the SMART on FHIR JavaScript Clientclient.

SMART on FHIR Authorization

...

After successful authorization, retrieve the resulting SMART FHIR Clientclient. The current user's fully qualified FHIR URL is on the FHIR client at userId. 

Ex: https://sandbox.hspconsortium.org/dstu2/open-hspc-reference-api/data/Practitioner/COREPRACTITIONER1

Code Block
languagejs
titleAuthorization Success
var currentUserFhirUrl;
 
FHIR.oauth2.ready(function(fhirClient){
	currentUserFhirUrl = fhirClient.userId;
});

Query User FHIR Resource

Retrieve the User's FHIR Resource resource (Patientpatient, Practitionerpractitioner, etc).

Code Block
languagejs
titleQuery Resource
var userIdSections = currentUserFhirUrl.split("/");

$.when(fhirClient.api.read({type: userIdSections[userIdSections.length-2], id: userIdSections[userIdSections.length-1]}))
    .done(function(userResult){

        var user = {name:""};
		if (userResult.data.resourceType === "Patient") {
    		var patientName = userResult.data && userResult.data.name && userResult.data.name[0];
    		user.name = patientName.given.join(" ") + " " + patientName.family.join(" ").trim();
		}
        user.id  = userResult.data.id;
    });