Miss any of our Open RFC calls?Watch the recordings here! »

eh-auth

0.3.5 • Public • Published

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/

Keywords

none

Install

npm i eh-auth

DownloadsWeekly Downloads

2

Version

0.3.5

License

MIT

Last publish

Collaborators

  • avatar
  • avatar