Skip to main content
Version: 2.x (Latest)

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
RenderDeploy to Renderdocs

For more information check docs

Step 2: Setup Instance

Start your Authorizer instance with the required CLI flags:

./build/server \
--database-type=sqlite \
--database-url=test.db \
--jwt-type=HS256 \
--jwt-secret=test \
--admin-secret=admin \
--client-id=123456 \
--client-secret=secret

Note the --client-id value -- you will need it in the SDK configuration below. Check Server Configuration for all available flags.

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 identifier (the value of --client-id flag used when starting the server)
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', // value of --client-id flag used to start the server
})

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', // value of --client-id flag used to start the server
})

// 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', // value of --client-id flag used to start the server
})

async function main() {
await authRef.login({
email: 'foo@bar.com',
password: 'test',
})
}