Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including eh-auth with all npm packages installed. Try it out:

var ehAuth = require("eh-auth")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

eh-auth v0.3.5

Employment Hero Authentication SDK

Auth JS

Auth flow

Request access token fail

Request api success

Access token expired and user session expired

Access token expired and create new token success

How to Use

Inject this code into your HTML file.

<script id="auth-sdk" src="../lib/EAuth.min.js" />
<script>
  var apiURL = <authServiceURL>;
  var eauth = new EAuth(apiURL);
  eauth.setRedirectLoginURL(<loginURL>);
  eauth.onTokenReceived = function(data) {
    console.log('data', data);
    // Save your token here
    <authDataHandler>
  }
  eauth.onTokenReceivedError = function(error) {
    console.log('data', data);
    <authDataErrorHandler>
  }

Your need to replace:

  • <authServiceURL> - URL to auth service
  • <loginURL> - Redirect URL when you get response 401
  • <authDataHandler> - Data handler, usually use to store access token to redux store
  • <authDataErrorHandler> - Beside redirect to login page, what do you want to do? Usually use to handle status code !== 401

Auth instance method

  • constructor(authURL) - Create Auth instance with auth service URL
  • setRedirectLoginURL - Set redirect link to login page. if requestToken or forceRequestToken response status is 401, it will redirect to login page
  • requestToken - Request token from local storage and validate it. If invalidate, call to Auth service to get new
  • forceRequestToken - call to Auth service to get new access token
  • onTokenReceived - Callback to handle access token return by requestToken or forceRequestToken
  • onTokenReceivedError - Callback to handle error return by requestToken or forceRequestToken. Note: if status is 401, it will redirect to login page.

Development

Scripts

  • npm run build - produces production version of your library under the lib folder
  • npm run diagrams - Re draw diagrams
  • npm run dev - produces development version of your library and runs a watcher
  • npm run test - well ... it runs the tests :)
  • npm run test:watch - same as above but in a watch mode

Demo

  pushstate-server ./

Go to http://localhost:9000/demo/

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free