Getting Started
@authorizerdev/authorizer-js
(opens in a new tab) is universal javaScript SDK for Authorizer API.
It supports:
- UMD (Universal Module Definition) (opens in a new tab) build for browsers
- CommonJS(cjs) (opens in a new tab) build for NodeJS version that don't support ES Modules
- ESM (ES Modules) (opens in a new tab) build for modern javascript standard, i.e. ES Modules
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 provider | One-click link | Additional information |
---|---|---|
Railway.app | docs (opens in a new tab) | |
Heroku | docs (opens in a new tab) | |
Render | docs (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
andDATABASE_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.
Key | Description |
---|---|
authorizerURL | Authorizer server endpoint |
redirectURL | URL to which you would like to redirect the user in case of successful login |
clientID | Your client id that is obtained from authorizer dashboard |
extraHeaders | Optional 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',
})
}