Skip to content

Authentication

Bazaar provides authentication out-of-the-box allowing you to create and authenticate users securely and easily.

Authorization is provided with the Permissions API.

Sign up and Log in

Sign up and log in are conveniently combined into a single method call:

bzr.login();

Calling the login method opens a pop-up window by default, which falls back to redirect login if the pop-up fails to open.

Optionally, you can set a login type:

import { LoginType } from "@bzr/bazaar";
bzr.login({ type: LoginType.REDIRECT });

Available LoginType are

  • POPUP_FALLBACK: Complete auth in a popup window but fall back to redirect if popup creation fails (default).
  • POPUP: Complete auth in a popup window.
  • REDIRECT: Complete auth via a redirect. This may case the site to loose context (temporary, unsaved data).

Log Out

A utility method to delete an auth token from local storage and perform a page refresh.

bzr.logOut();

Check if a User is Logged In

A utility method to check if an auth token is set in local storage.

bzr.isLoggedIn();

Handle a Successful Login Event

Logging in takes place without a page fresh, so you need a way to handle a successful login event. To do so, set a callback as the first and only argument of onLogin:

bzr.onLogin(() => {
// Handle user log in
// e.g. Set local state and redirect
});

Handle a Login Error Event

Handle a login error event:

bzr.onLoginError(() => {
// Handle log in error
// e.g. Show error message
});