Getting Started

@authorizerdev/authorizer-js is universal javaScript SDK for Authorizer API. It supports:

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
HerokuDeploy to Herokudocs
Renderrender buttondocs

For more information check docs

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 res = await authorizerRef.authorize({
response_type: 'code',
use_refresh_token: false,
})
if (res && res.access_token) {
// you can use user information here, eg:
const user = await authorizerRef.getProfile({
Authorization: `Bearer ${res.access_token}`,
})
const userSection = document.getElementById('user')
const logoutSection = document.getElementById('logout-section')
logoutSection.classList.toggle('hide')
userSection.innerHTML = `Welcome, ${user.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',
})
}