1.1.0 • Published 1 year ago

@unologin/web-sdk v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Frontend SDK for interfacing with unolog·in.

This README is a short overview. The full documentation can be found here.

Documentation for other unolog·in packages can be found here.

Installation

npm install @unologin/web-sdk

or

yarn add @unologin/web-sdk

Typescript

This package includes built-in type declarations. There is no need to install any additional packages.

The below examples will use plain javascript for generality.

Setup

Before using the library, make sure to connect the SDK with your app via your apps appId.

import unologin from '@unologin/web-sdk';

unologin.setup(
  { appId: process.env.UNOLOGIN_APPID }
);

Initiating the login flow

Use the startLogin(...) function in order to initiate the login flow.

import unologin from '@unologin/web-sdk';

/**
 * Typically, you would call startLogin in
 * the onClick handler of a button.
 * 
 * startLogin returns a promise that 
 * resolves as soon as the popup closes.
 * 
 * startLogin can therefore be awaited.
 */
unologin.startLogin(
  {
    /**
     * user class this 
     * login/registration belongs to 
     */
    userClass: 'users_default',
    /**
     * mode can be 'login' or 'register'
     * or omitted for auto-detect 
     */
    mode: 'register'
  }
)

Handling the login event

unologin.onLogin returns a Promise which can either be awaited or used with .then/.catch.

unologin.startLogin(...).then(() => 
{
  /** 
   * you can now make 
   * authenticated requests! 
   */
  alert('You are now logged in!');
}).catch((e) => 
{
  /* Check the error type. */
  if (e.type === 'ClosedByUser')
  {
    /**
     * Decide what happens if the user
     * quits the login flow.
     */
  }
  else 
  {
    alert('Something went wrong!');
  }
})

Detecting a login cookie

You can use unologin.isLoggedIn() to check if a user is logged in at any time. This will check for the presence of a javascript-readable state cookie that is set along side the actual login cookie.

IMPORTANT: Do not use isLoggedIn() or any other client-side logic as a security measure.

Authentication should always be performed from your backend.

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago