Skip to content

Universal javaScript SDK for Authorizer API

License

Notifications You must be signed in to change notification settings

siimsams/authorizer-js

 
 

Repository files navigation

Authorizer.js

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

All the above versions require Authorizer instance to be instantiated and used. Instance constructor requires an object with the following keys

Key Description
authorizerURL Authorizer server endpoint
redirectURL URL to which you would like to redirect the user in case of successful login

Example

const authRef = new Authorizer({
  authorizerURL: "https://app.herokuapp.com",
  redirectURL: window.location.origin,
});

IIFE

  • 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: `AUTHORIZER_URL`,
    redirectURL: window.location.origin,
    clientID: "YOUR_CLIENT_ID", // can be obtained 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) {
      // get user profile using the access token
      const user = await authorizerRef.getProfile({
        Authorization: `Bearer ${res.access_token}`,
      });

      // 	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/authoirzer-js
  • Step 2: Import and initialize the authorizer instance
const { Authorizer } = require("@authorizerdev/authoirzer-js");

const authRef = new Authorizer({
  authorizerURL: "https://app.heroku.com",
  redirectURL: "http://app.heroku.com/app",
});

async function main() {
  await authRef.login({
    email: "[email protected]",
    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: "https://app.heroku.com",
  redirectURL: "http://app.heroku.com/app",
});

async function main() {
  await authRef.login({
    email: "[email protected]",
    password: "test",
  });
}

Local Development Setup

Prerequisites

Setup

  • Clone the repository
  • Install dependencies using pnpm install
  • Run pnpm build to build the library
  • Run pnpm test to run the tests

Release

  • Run pnpm release to release a new version of the library

About

Universal javaScript SDK for Authorizer API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 71.6%
  • JavaScript 18.7%
  • HTML 9.6%
  • Shell 0.1%