authorizer-js
Getting Started

Getting Started

@authorizerdev/authorizer-js (opens in a new tab) is universal javaScript SDK for Authorizer API. It supports:

Migration Guide from 1.x -> 2.x

2.x version of @authorizerdev/authorizer-js has a uniform response structure that will help your applications to get right error codes and success response. Methods here have {data, errors} as response objects for methods of this library.

For 1.x version of this library you can get only data in response and error would be thrown so you had to handle that in catch.

Here is a quick guide on getting started with @authorizerdev/authorizer-js package.

Step 1: Get Authorizer Instance

Deploy production ready Authorizer instance using one click deployment options available below

Infra providerOne-click linkAdditional information
Railway.appDeploy on Railwaydocs (opens in a new tab)
HerokuDeploy to Herokudocs (opens in a new tab)
Renderrender buttondocs (opens in a new tab)

For more information check docs (opens in a new tab)

Step 2: Setup Instance

  • Open authorizer instance endpoint in browser
  • Sign up as an admin with a secure password
  • Configure environment variables from authorizer dashboard. Check env docs for more information

Note: DATABASE_URL, DATABASE_TYPE and DATABASE_NAME are only configurable via platform envs

Step 3 - Install SDK

Load the @authorizerdev/authorizer-js library and initialize the authorizer object. Authorizer object can be instantiated with JSON object with following keys in its constructor.

KeyDescription
authorizerURLAuthorizer server endpoint
redirectURLURL to which you would like to redirect the user in case of successful login
clientIDYour client id that is obtained from authorizer dashboard
extraHeadersOptional JSON object that you can pass to send extra headers to your gateway / authorizer server

Example

const authRef = new Authorizer({
  authorizerURL: 'YOUR_AUTHORIZER_INSTANCE_URL',
  redirectURL: window.location.origin,
  clientID: 'YOUR_CLIENT_ID', // obtain your client id from authorizer dashboard
})

UMD

  • Step 1: Load Javascript using CDN
<script src="https://unpkg.com/@authorizerdev/authorizer-js/lib/authorizer.min.js"></script>
  • Step 2: Use the library to instantiate Authorizer instance and access various methods
<script type="text/javascript">
  const authorizerRef = new authorizerdev.Authorizer({
    authorizerURL: `YOUR_AUTHORIZER_INSTANCE_URL`,
    redirectURL: window.location.origin,
    clientID: 'YOUR_CLIENT_ID', // obtain your client id from authorizer dashboard
  })
 
  // use the button selector as per your application
  const logoutBtn = document.getElementById('logout')
  logoutBtn.addEventListener('click', async function () {
    await authorizerRef.logout()
    window.location.href = '/'
  })
 
  async function onLoad() {
    const { data, errors } = await authorizerRef.authorize({
      response_type: 'code',
      use_refresh_token: false,
    })
    if (data && data.access_token) {
      // you can use user information here, eg:
      const user = await authorizerRef.getProfile({
        Authorization: `Bearer ${data.access_token}`,
      })
      const userSection = document.getElementById('user')
      const logoutSection = document.getElementById('logout-section')
      logoutSection.classList.toggle('hide')
      userSection.innerHTML = `Welcome, ${user.data.email}`
    }
  }
  onLoad()
</script>

CommonJS

  • Step 1: Install dependencies
npm i --save @authorizerdev/authorizer-js
OR
yarn add @authorizerdev/authorizer-js
  • Step 2: Import and initialize the authorizer instance
const { Authorizer } = require("@authorizerdev/authorizer-js");
 
const authRef = new Authorizer({
  authorizerURL: "https://app.heroku.com",
  redirectURL: "http://app.heroku.com/app",
  clientID:
});
 
async function main() {
  await authRef.login({
    email: "foo@bar.com",
    password: "test",
  });
}

ES Modules

  • Step 1: Install dependencies
npm i --save @authorizerdev/authorizer-js
OR
yarn add @authorizerdev/authorizer-js
  • Step 2: Import and initialize the authorizer instance
import { Authorizer } from '@authorizerdev/authorizer-js'
 
const authRef = new Authorizer({
  authorizerURL: 'AUTHORIZER_URL',
  redirectURL: 'YOUR_APP',
  clientID: 'YOUR_CLIENT_ID', // obtain your client id from authorizer dashboard
})
 
async function main() {
  await authRef.login({
    email: 'foo@bar.com',
    password: 'test',
  })
}