BkperAuth
OAuth authentication client for the Bkper API.
Provides framework-agnostic authentication with callback-based event handling. Access tokens are stored in-memory; sessions persist via HTTP-only cookies.
Example
// Initialize authentication clientconst auth = new BkperAuth({ onLoginSuccess: () => loadUserData(), onLoginRequired: () => showLoginButton()});
// Restore session on app loadawait auth.init();Constructors
Constructor
new BkperAuth(config?): BkperAuth;Creates a new BkperAuth instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
config? | BkperAuthConfig | Optional configuration for the auth client |
Returns
BkperAuth
Example
// Simple usage with defaultsconst auth = new BkperAuth();
// With callbacksconst auth = new BkperAuth({ onLoginSuccess: () => console.log('Logged in!'), onLoginRequired: () => showLoginDialog(), onError: (error) => console.error(error)});Methods
getAccessToken()
getAccessToken(): string | undefined;Gets the current access token.
Returns
string | undefined
The access token if authenticated, undefined otherwise
Example
const token = auth.getAccessToken();if (token) { // Make authenticated API calls fetch('/api/data', { headers: { 'Authorization': `Bearer ${token}` } });}init()
init(): Promise<void>;Initializes the authentication state by attempting to refresh the access token.
Call this method when your app loads to restore the user’s session.
Triggers onLoginSuccess if a valid session exists, or onLoginRequired if login is needed.
Returns
Promise<void>
login()
login(): void;Redirects the user to the login page.
The user will be redirected to the authentication service to complete the login flow. After successful login, they will be redirected back to the current page.
Returns
void
Example
// Trigger login when user clicks a buttonloginButton.addEventListener('click', () => { auth.login();});logout()
logout(): void;Logs out the user and redirects to the logout page.
Triggers the onLogout callback before redirecting.
The user’s session will be terminated.
Returns
void
Example
// Logout when user clicks logout buttonlogoutButton.addEventListener('click', () => { auth.logout();});refresh()
refresh(): Promise<void>;Refreshes the access token using the current session.
Call this when API requests return 403 to get a new token and retry.
Triggers onTokenRefresh callback if successful.
Throws error if the refresh fails (network error, expired session, etc.).
Returns
Promise<void>
Example
// Handle 403 by refreshing and retryingconst response = await fetch('/api/data', { headers: { 'Authorization': `Bearer ${auth.getAccessToken()}` }});
if (response.status === 403) { await auth.refresh(); // Retry with new token return fetch('/api/data', { headers: { 'Authorization': `Bearer ${auth.getAccessToken()}` } });}