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

@witsauth/js-client

v1.0.0

Published

Official pure JavaScript/TypeScript client library for integrating Witsauth Single Sign-On (SSO) authentication into any JavaScript application.

Downloads

72

Readme

@witsauth/js-client

The official pure JavaScript/TypeScript client library for integrating WitsAuth Single Sign-On (SSO) authentication into any JavaScript application.

This SDK is completely framework-agnostic. It works seamlessly with Vanilla JS, Vue, Svelte, Angular, or any other modern web framework without relying on React or external dependencies like Axios.

Features

  • 100% Framework Agnostic: Pure JavaScript and TypeScript.
  • Zero Dependencies: Extremely lightweight bundle using native browser APIs (fetch, crypto).
  • OAuth 2.0 & PKCE: Secure authorization code flow with Proof Key for Code Exchange built-in.
  • Auto-Refresh: Automatically handles token rotation and refreshing behind the scenes.
  • Event-Driven: Easy subscription model to react to authentication state changes.

Installation

npm install @witsauth/js-client
# or
pnpm install @witsauth/js-client
# or
yarn add @witsauth/js-client

Basic Usage

1. Initialize the Client

Create a single instance of WitsAuthClient and configure it with your WitsAuth credentials.

import { WitsAuthClient } from '@witsauth/js-client';

const authClient = new WitsAuthClient({
  clientId: 'your-client-id',
  authorizationEndpoint: 'https://auth.yourdomain.com/authorize',
  tokenEndpoint: 'https://auth.yourdomain.com/token',
  redirectUri: 'http://localhost:3000/callback',
  // Optional configuration
  autoRefresh: true,
  refreshThreshold: 60, // Refresh 60 seconds before expiry
  logLevel: 'warn'
});

// Initialize the client (checks existing tokens and starts auto-refresh if applicable)
authClient.init();

2. Subscribe to State Changes

You can listen for authentication state changes to update your UI dynamically.

const unsubscribe = authClient.subscribe((state) => {
  if (state.isLoading) {
    console.log('Authentication in progress...');
  } else if (state.isAuthenticated) {
    console.log('Logged in successfully!');
    console.log('Access Token:', state.tokenInfo.accessToken);
  } else {
    console.log('User is logged out.');
  }
});

3. Trigger Login

Call .login() to redirect the user to the WitsAuth SSO page.

document.getElementById('login-btn').addEventListener('click', () => {
  authClient.login();
});

4. Handle the Callback

On your designated callback page (e.g., http://localhost:3000/callback), capture the code and state parameters from the URL and pass them to the client.

// On your /callback route:
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
const state = urlParams.get('state');

if (code && state) {
  authClient.handleCallback(code, state)
    .then(tokenInfo => {
      // Successfully authenticated!
      // The client will automatically redirect to your configured `redirectRoute` if set.
      window.location.href = '/dashboard';
    })
    .catch(err => {
      console.error('Authentication failed:', err);
    });
}

5. Making Authenticated API Calls

Whenever you need to call your backend API, simply retrieve the current token from the client and inject it into your headers.

async function fetchSecureData() {
  const token = authClient.getAccessToken();
  
  if (!token) {
    throw new Error("No access token available!");
  }

  const response = await fetch('https://api.yourdomain.com/data', {
    headers: {
      'Authorization': `Bearer ${token}`
    }
  });

  return response.json();
}

6. Logout

document.getElementById('logout-btn').addEventListener('click', () => {
  authClient.logout();
});

License

GPL-3.0