@bzr/bazaar
Version:
The Bazaar SDK. Conveniently use Bazaar with your app in the browser.
116 lines (115 loc) • 3.89 kB
TypeScript
import type { AuthOptions } from "../types";
import { LoginType } from "../types";
/**
* The class that deals with login and authentication
*/
export declare class Auth {
/**
* Local storage key names, namespaced in the constructor
*/
private tokenKeyName;
private pkceStateKeyName;
private pkceCodeVerifierKeyName;
private oAuthClient;
/**
* An app's base URL
* Used to check against the origin of a postMessage event sent from the login pop-up window.
* e.g. https://example-app.com
*/
private baseUrl;
/**
* The OAuth2 redirect URI. Required to get token.
*/
private loginRedirectUri;
/**
* A reference to the window object of the login pop-up window.
*/
private popupWindow;
private popupWindowName;
/**
* A reference to the previous URL of the login pop-up window.
* Used to avoid creating duplicate windows and for focusing an existing window.
*/
private popupPreviousUrl;
/**
* A reference to a {@link popupMessageListener} so it can be reliably cleaned up
*/
private boundPopupMessageListener;
/**
* Set from {@link login} context so errors from {@link popupMessageListener} can propagate.
*/
private popupResolve;
/**
* Set from {@link login} context so errors from {@link popupMessageListener} can propagate.
*/
private popupReject;
/**
* A callback function an app can specify to run when a user has successfully logged in.
*
* e.g. Set state, redirect, etc.
*/
onLogin: () => void;
/**
* A callback function an app can run a login error occurs.
*
* e.g. Authorization code is invalid
*/
onLoginError: (message: string) => void;
constructor(options: AuthOptions, onLogin: () => void, onLoginError: (message: string) => void);
/**
* Generates a URI to log in a user to Bazaar and authorize an app.
* Uses the Authorization Code Flow for single page apps with PKCE code verification.
* Requests an authorization code.
*/
loginUri(): Promise<string>;
/**
* Opens a pop-up window to perform OAuth login.
* Will fallback to redirect login if pop-up fails to open, if `options.type` is not `popup` (meaning an app has explicitly opted out of falling back to redirect login)
*/
login(options?: {
type?: LoginType;
}): Promise<void>;
/**
* A "message" event listener for the login pop-up window.
* Handles messages sent from the login pop-up window to its opener window.
* Set to {@link boundPopupMessageListener} in the constructor
*/
private popupMessageListener;
/**
* Continues the login flow after redirected back from the OAuth server, handling pop-up or redirect login types.
*
* Must be called at the {@link loginRedirectUri} URI.
*
* @returns string to indicate login type
*/
private checkLoginQueryParams;
/**
* Completes a login request
*
* Takes an authorization code and exchanges it for an access token.
*
* Expects `code` and `state` query params to be present in the URL. Or else an `error` query
* param if something went wrong.
*
* Stores the access token in local storage.
*
* Performs after login actions.
*/
private completeLogin;
/**
* Check if the user is logged in.
* i.e. if an access token is in local storage.
*/
isLoggedIn(): boolean;
/**
* Checks if a redirect to complete a login request has been performed.
*
* Also used in {@link loginUri} to make sure PKCE local storage values are not overwritten,
* which would otherwise accidentally invalidate a login request.
*/
static hasLoginQueryParams(): boolean;
/**
* Logs out a user.
*/
logOut(): void;
}