Connect Session

An NPM package to simplify OAuth with Reapit Connect

We are aware that managing OAuth flows can be tricky, especially redirecting, keeping sessions refreshed and cached. To make this process easier, we have built the Connect Session module for any JavaScript app.

To get started run yarn add @reapit/connect-session

Then follow the steps for either browsers, React or NodeJS below.

Basic Browser Usage

The module is intended to be browser framework agnostic although we ship a React Hook for React users (see below).

For all users, in a file at the root of the source of your project, first instantiate and export the ReapitConnectBrowserSession class.

The constructor accepts 5 parameters, of which two are optional, see comments below. The production connectUserPoolId is eu-west-2_eQ7dreNzJ .

import { ReapitConnectBrowserSession } from '@reapit/connect-session'

// You should instantiate the class once only as a singleton as the module manages it's own state
export const reapitConnectBrowserSession = new ReapitConnectBrowserSession({
  // The client id of your application, obtained from Reapit Developer Portal
  connectClientId: 'SOME_CLIENT_ID',
  // The url to the Reapit Connect instance. While in beta this is the below URL but will need to be context aware in full prod/
  connectOAuthUrl: 'https://connect.reapit.cloud',
  // OAuth UserPoolId - refer to the foundations documentation to obtain this for the correct environment
  connectUserPoolId: 'SOME_USER_POOL_ID',
  // The relative path you want to re-direct in your application after a successful login. You will have supplied this when you registered your app.
  // Defaults to '' or the root of your project if not supplied
  connectLoginRedirectPath: '/some-redirect-path',
  // The relative path you want to re-direct in your application after a successful logout. You will have supplied this when you registered your app.
  // Defaults to '/login' if not supplied
  connectLogoutRedirectPath: '/some-login-path',
  // The time in ms before your session times out when a user is inactive. 
  // Defaults to 10800000 - 3hrs
  connectApplicationTimeout: 10800000
})

The instantiated class can then be used in your code. It exports the following methods:

React Usage

In addition to the basic browser API, we export a React Hook to use in your React Components.

To leverage the Hook, first instantiate the class as per above. Then, around the Routes you wish to protect with Reapit Connect authentication, simply return when the session is not present as below:

Then in my React child components, I have access to the session values and methods eg:

Sign In With Reapit Button

Perhaps the simplest way to authenticate on the client side is to embed the "Sign In With Reapit Button" on your page. This is not exported from the NPM package as it is distributed via a CDN however, you can see full documentation here.

Node Usage

For server side usage, we also export a Node module with a stripped down API that simply returns a promise from a connectAccessToken method. For a basic and slightly contrived example, see the simple Express app below:

As per the browser usage, you will need to instantiate the class with your initialisers, in this case connectClientId, connectOAuthUrl (in the same way as the browser module), but with the addition of the connectClientSecret you obtain from your app listing page.

The module will fetch and refresh your session as the token expires, caching it in session storage to minimise calls to Reapit Connect token endpoint. After closing a tab, the session cache is not persisted.

Last updated