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 client
const auth = new BkperAuth({
onLoginSuccess: () => loadUserData(),
onLoginRequired: () => showLoginButton()
});
// Restore session on app load
await auth.init();
string
|undefined
Gets the current access token.
Returns 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}` }
});
}
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.
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.
example
// Trigger login when user clicks a button
loginButton.addEventListener('click', () => {
auth.login();
});
void
Logs out the user and redirects to the logout page.
Triggers the onLogout callback before redirecting.
The user’s session will be terminated.
example
// Logout when user clicks logout button
logoutButton.addEventListener('click', () => {
auth.logout();
});
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.).
example
// Handle 403 by refreshing and retrying
const 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()}` }
});
}