Skip to content

olafsh/olaf-sdk-js

Repository files navigation

OLAF SDK for JS

Overview

OLAF SDK for JS facilitates seamless integration of OLAF.sh into your apps, offering features such as:

  • 🗃️ secure credential storage,
  • 🔐 effortless login and logout processes,
  • 👤 and convenient access to user information, styles and permissions.

Installation

// via npm
npm install @olafsh/olaf-sdk-js

// via yarn
yarn add @olafsh/olaf-sdk-js

Initialize SDK

SDK can be imported either via CommonJS or ES Modules methods.

CommonJS syntax

const OLAFSDK = require('@olafsh/olaf-sdk-js');

const sdk = new OLAFSDK();

ES Modules syntax

import OLAFSDK from "@olafsh/olaf-sdk-js";

const sdk = new OLAFSDK();

Warning

The SDK requires a browser to perform the authentication process, hence it CAN NOT be used in a NodeJS environment.

Check examples/ directory for more usages.

Usage

// Fetch configuration for your app from olaf.sh platform
await sdk.fetchConfig()

// Set language for sign in on olaf.sh platform
sdk.setLanguage('en');

// Perform a login with redirect
await sdk.loginWithRedirect();

// OPTIONAL: Build authorize url for manual redirect
const authorizeUrl = await sdk.buildAuthorizeUrl();

// Handle login callback from olaf.sh platform
// HINT: It's usually the best to done this on a different route
await sdk.handleRedirectCallback()

// Check current authentication status
console.log(await sdk.isAuthenticated);

// Get access token
const accessToken = sdk.accessToken;

// OPTIONAL: Get user details
const user = await sdk.me();

// Perform a logout
await sdk.logout();

Development

All source code is located in /src. The source code is compiled via npm run build command to /lib directory.

# Clone project
git clone [email protected]:olafsh/olaf-sdk-js.git

# Compile changes
npm run build
    
# Run test
npm run test