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.

Log in and Sign up 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 cause the site to lose context (temporary, unsaved data).

With a popup, login settles when the login finished or the user closed the popup. Check isLoggedIn to tell them apart. It rejects if the popup couldn’t open and the type is POPUP.

The page must be on the origin of your loginRedirectUri. Otherwise login opens nothing and calls the login error callback.

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

bzr.logOut();

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

bzr.isLoggedIn();

Logging in takes place without a page refresh, 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. The callback receives the BazaarApp instance:

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

Handle a login error event:

bzr.onLoginError(async (bzr, message) => {
// Handle log in error
// e.g. Show error message
console.error("Login error:", message);
});