Getting Started
@authorizerdev/authorizer-js
is universal javaScript SDK for Authorizer API.
It supports:
- UMD (Universal Module Definition) build for browsers
- CommonJS(cjs) build for NodeJS version that don't support ES Modules
- ESM (ES Modules) build for modern javascript standard, i.e. ES Modules
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 | |
Heroku | docs | |
Render | docs |
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
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 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',
})
}
Last updated on August 31, 2022