npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bkper/web-auth

v1.1.0

Published

Framework-agnostic OAuth authentication SDK for Bkper API

Readme

@bkper/web-auth

OAuth authentication SDK for apps on the Bkper Platform (*.bkper.app subdomains).

npm

Documentation

Installation

bun add @bkper/web-auth
npm i -S @bkper/web-auth
pnpm add @bkper/web-auth
yarn add @bkper/web-auth

Quick Start

import { BkperAuth } from '@bkper/web-auth';

// Initialize client with callbacks
const auth = new BkperAuth({
    onLoginSuccess: () => {
        console.log('User authenticated!');
        loadUserData();
    },
    onLoginRequired: () => {
        console.log('Please sign in');
        showLoginButton();
    },
});

// Initialize authentication flow on app load
await auth.init();

// Make an authenticated request with automatic token refresh and one retry
const response = await auth.authenticatedFetch('/data');

Authenticated Requests

authenticatedFetch() implements the standard Fetch API contract. It adds the current bearer token to a request. If the response is 401, it refreshes the token and retries exactly once. Other response statuses are returned unchanged.

const response = await auth.authenticatedFetch('/data', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ value: 42 }),
});

The method can also be supplied to any HTTP client that accepts a Fetch-compatible function:

const fetchWithAuth = auth.authenticatedFetch.bind(auth);

Call init() before the first authenticated request. If no token is available, or the session cannot be refreshed, onLoginRequired is called and the request rejects with an authentication-required error. If the retried request also returns 401, that response is returned without another retry. Concurrent refresh calls share one refresh request.

To prevent accidental token disclosure, authenticated requests are restricted to:

  • HTTPS origins on bkper.app or its subdomains
  • The current localhost or 127.0.0.1 origin during local development

Request paths are not restricted.

Using with bkper-js

@bkper/web-auth does not depend on bkper-js, but they can be connected through the client configuration. Provide the current token for each request and refresh it when the Bkper API reports an expired login:

import { Bkper } from 'bkper-js';

const bkper = new Bkper({
    oauthTokenProvider: async () => auth.getAccessToken(),
    requestRetryHandler: async (status, _error, attempt) => {
        if (status === 403 && attempt === 1) {
            await auth.refresh();
        }
    },
});

bkper-js owns its request and retry lifecycle. @bkper/web-auth remains responsible only for the current access token and session refresh.

What's Included

  • OAuth authentication SDK for apps on *.bkper.app subdomains
  • Callback-based API for authentication events
  • OAuth flow with in-memory token management
  • Single-flight token refresh mechanism
  • Authenticated Fetch API with one-time refresh and retry
  • TypeScript support with full type definitions

How It Works

Session Persistence:

  • Access tokens are stored in-memory (cleared on page refresh)
  • Sessions persist via HTTP-only cookies scoped to the .bkper.app domain
  • Call init() on app load to restore an access token from the session
  • Protected resources still require Authorization: Bearer <token>; session cookies only restore client auth state

Note: This SDK only works for apps hosted on *.bkper.app subdomains. Applications on other domains must provide a valid access token through their own authentication mechanism.

Security:

  • HTTP-only cookies protect refresh tokens from XSS
  • In-memory access tokens minimize exposure

TypeScript Support

This package is written in TypeScript and provides full type definitions out of the box. All public APIs are fully typed, including callbacks and configuration options.

import { BkperAuth, BkperAuthConfig } from '@bkper/web-auth';

const config: BkperAuthConfig = {
    onLoginSuccess: () => console.log('Authenticated'),
    onError: error => console.error('Auth error:', error),
};

const auth = new BkperAuth(config);

Browser Compatibility

This package requires a modern browser with support for:

The app must be deployed to a *.bkper.app subdomain for session-cookie token restoration to work.

API Reference

BkperAuth

The main authentication client class.

Constructor

new BkperAuth(config?: BkperAuthConfig)

Methods

  • init(): Promise<void> - Initialize auth state by attempting to refresh the token. Triggers onLoginSuccess if successful, onLoginRequired if authentication is needed, or onError if refresh fails. Call on app load.
  • login(): void - Request the start of the login flow.
  • refresh(): Promise<void> - Refresh the access token. Concurrent calls share one refresh request. Triggers onTokenRefresh if successful or onError if refresh fails.
  • authenticatedFetch(input, init?): Promise<Response> - Send an authenticated Fetch API request, refreshing and retrying once after 401.
  • logout(): void - Request the start of the logout flow. Triggers onLogout callback.
  • getAccessToken(): string | undefined - Get the current access token.

BkperAuthConfig

Configuration options for the auth client.

Properties

  • baseUrl?: string - Override the authentication service URL (for testing/development).
  • onLoginSuccess?: () => void - Called when login succeeds.
  • onLoginRequired?: () => void - Called when login is required.
  • onLogout?: () => void - Called when user logs out.
  • onTokenRefresh?: (token: string) => void - Called when token is refreshed.
  • onError?: (error: unknown) => void - Called when an auth error occurs.
  • getAdditionalAuthParams?: () => Record<string, string> - Provide additional parameters for auth requests.

License

Apache-2.0