/
How to Get the Current User

How to Get the Current User

Introduction


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

SMART on FHIR Authorization

Request Authorization

In order to receive the current user, you must request the openid and profile scopes.

Authorize
FHIR.oauth2.authorize({
    client_id: "my_client_id",
    redirect_uri: "https://mydomain.com/app/index.html",
    scope: "patient/*.read openid profile"
});

Authorization Success

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

Ex: https://open-api.logicahealth.org/data/Practitioner/COREPRACTITIONER1

Authorization Success
var currentUserFhirUrl;
 
FHIR.oauth2.ready(function(fhirClient){
	currentUserFhirUrl = fhirClient.userId;
});

Query User FHIR Resource

Retrieve the User's FHIR resource (patient, practitioner, etc).

Query 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;
    });

Related content

Authorization using OAuth2
Authorization using OAuth2
More like this
Use the fhir-client.js in a Multi-page Architecture
Use the fhir-client.js in a Multi-page Architecture
More like this
How to Update a Patient Record
How to Update a Patient Record
More like this
Samples
Read with this
OAuth2 Authorization Walk-through
OAuth2 Authorization Walk-through
More like this
Quick Start
Quick Start
Read with this