Skip to content

Commit

Permalink
Merge pull request #21 from TopologyHealth/better-auth-redirect
Browse files Browse the repository at this point in the history
Better-auth-redirect
  • Loading branch information
oyisre authored Apr 10, 2024
2 parents 5321d81 + f834ce2 commit 73ba46d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 172 deletions.
92 changes: 0 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,98 +22,6 @@ Using npm's git repo support: `npm install [email protected]:TopologyHealth/SMARTer

Support for the npm registry will be coming soon

<!-- npm: -->
<!--
```bash
npm install smarter-fhir
``` -->

## Usage

Here's a basic example demonstrating how to use the SMARTerFHIR library:

### Web Launch (using EMR or Standalone Launch)

The following is an example of a function to handle the SMART Launch. I.e., when the EMR launches the application, it must be directed to the page that runs the following. E.g., this could be https://www.yourwebsite.com/launch

```typescript
async function handleWebLaunch() {
try {
const emrClientID = YOUR_EMR_CLIENT_ID
const emrClientSecret = EMR_CLIENT_SECRET //OPTIONAL -> Only Required if your EMR Authentication was registered with a Secret
const smartLaunchHandler = new SmartLaunchHandler(emrClientID, emrClientSecret) //If no secret, use 'new SmartLaunchHandler(emrClientID)'
await smartLaunchHandler.authorizeEMR(LAUNCH.EMR) //If Standalone, use 'await smartLaunchHandler.authorizeEMR(LAUNCH.STANDALONE)'
}
catch (e) {
if (e instanceof Error) {
throw e;
}
throw new Error(`Unknown Error: ${e}`)
}
}
```

Here’s a breakdown of the above code snippet:

* `emrClientID`: Pass in the Client ID of the EMR Client that you want to connect to
* `emrClientSecret`: Pass in the Client Secret of the EMR Client that you want to connect to
* `SmartLaunchHandler`: Our Handler Object. Used mainly with authorizeEMR() to authorize the app and redirect accordingly

### Standalone Launch Details (Only for Standalone Launch. Skip if doing EMR Launch)
Relevant if using Standalone Launch.

When running `handleWebLaunch` for Standalone Launch, an `iss` query parameter must be passed to the webpage.

To aid in simplicity, you can use this example button that navigates to the page that runs `handleWebLaunch` and passes in the appropriately required `iss`:
```typescript
<Button my="sm" onClick={startStandaloneLaunch}>
{"Execute Standalone Launch"}
</Button>

const startStandaloneLaunch = () =>
{
const emrType = ((YOUR_REACT_APP_EMR_TYPE).toLowerCase() as EMR) //YOUR_REACT_APP_EMR_TYPE must be set to 'EPIC' or 'CERNER' (More EMRs to come!)
const emrEndpoints = getEndpointsForEmr(emrType)
const iss = emrEndpoints.r4.toString()
navigate({
pathname: '/standalonelaunch', //This pathname should make a request to the page that runs 'handleWebLaunch'
search: `?iss=${iss}`,
});
}
```

### SMART Client

The following is an example of a function to instantiate the SMART Client after `SMART Launch` has completed. During SmartLaunch, the EMR will authenticate your application. Once completed, it will redirect to the assigned redirect url. The code below should be set to run upon successful authentication and redirect:

```typescript
async function mySmartClientInstantiator() {
try {
const clientFactory = new ClientFactory();
const client = await clientFactory.createEMRClient(LAUNCH.EMR) //If Standalone, use 'await clientFactory.createEMRClient(LAUNCH.STANDALONE)'
if (!client) throw new Error('no client found')
return client
} catch (reason) {
if (!(reason instanceof Error))
throw new Error(`Unknown Error: ${reason}`)
if (reason.message.includes("No 'state' parameter found. Please (re)launch the app.")
|| reason.message.includes("Could not find any JWT token")
|| reason.message === NO_CODE) {
return console.log('No EMR connection established.')
}
if (reason.message.includes("User is not available"))
return console.log('Waiting for Web Launch...')
console.error(reason.message)
}
}
```
Code snippet breakdown:

* `clientFactory`: Object for creating new EMRClients post-SMART authentication
* `client`: Client object for performing operations against the EMR for writing/receiving data

Make sure to import the necessary classes, interfaces, and types based on your requirements.

## Documentation

For detailed documentation on the SMARTerFHIR library, including classes, methods, and usage examples, please refer to the official documentation at [https://topology.health/docs](https://topology.health/docs).
Expand Down
2 changes: 1 addition & 1 deletion lib/Launcher/SmartLaunchHandler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class SmartLaunchHandler {
* Authorizes the EMR based on the current URL query parameters.
* @returns {Promise<void>} - A promise resolving to void.
*/
authorizeEMR(launchType?: LAUNCH): Promise<void>;
authorizeEMR(launchType?: LAUNCH, redirectPath?: string): Promise<void>;
/**
* The function `executeEMRLaunch` checks the URL parameters for an "iss" value, determines the EMR type based on the "iss" value, and then launches the
* corresponding EMR system.
Expand Down
21 changes: 13 additions & 8 deletions lib/Launcher/SmartLaunchHandler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Launcher/SmartLaunchHandler.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 73ba46d

Please sign in to comment.