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

@shyam-148/auth-client

v1.0.1

Published

Framework-agnostic auth client core

Readme

Auth Client

A framework-agnostic authentication client for TypeScript/JavaScript applications. Provides a robust, type-safe API for handling user authentication with support for email/password login, MFA, token refresh, and session management.

Features

  • 🔐 Multiple Authentication Strategies - Email/Password with extensible strategy pattern
  • 🔄 Automatic Token Refresh - Seamless token renewal with request queuing
  • 📱 MFA Support - Built-in support for multi-factor authentication flows
  • 💾 Persistent Sessions - Automatic session restoration from storage
  • 📡 Reactive State - Subscribe to authentication state changes
  • 🛡️ Type-Safe - Full TypeScript support with comprehensive types
  • 🎯 Framework Agnostic - Works with React, Vue, Angular, or vanilla JS

Installation

npm install auth-client
# or
pnpm add auth-client
# or
yarn add auth-client

Quick Start

Basic Usage

import { AuthManager, EmailPasswordStrategy } from 'auth-client';

// Create the auth manager
const auth = new AuthManager({
  baseUrl: 'https://api.example.com',
  applicationSlug: 'my-app',
});

// Create the email/password strategy
const emailStrategy = new EmailPasswordStrategy('my-app');

// Login
try {
  await auth.login(emailStrategy, {
    email: '[email protected]',
    password: 'password123'
  });
  
  console.log('Logged in!', auth.getUser());
} catch (error) {
  console.error('Login failed:', error);
}

// Check authentication status
if (auth.isAuthenticated()) {
  console.log('User:', auth.getUser());
}

// Logout
await auth.logout();

Using the Factory Function

import { createAuthClient } from 'auth-client';

const { manager, emailPasswordStrategy } = createAuthClient({
  baseUrl: 'https://api.example.com',
  applicationSlug: 'my-app',
  debug: true, // Enable debug logging
});

// Login using the pre-configured strategy
await manager.login(emailPasswordStrategy, {
  email: '[email protected]',
  password: 'password123'
});

Subscribing to State Changes

const unsubscribe = auth.subscribe((snapshot, event) => {
  console.log('Auth event:', event);
  console.log('Status:', snapshot.status);
  console.log('User:', snapshot.user);
});

// Later, unsubscribe when no longer needed
unsubscribe();

Handling MFA

import { 
  createAuthClient, 
  isMfaChallenge, 
  MfaRequiredError 
} from 'auth-client';

const { manager, emailPasswordStrategy } = createAuthClient({
  baseUrl: 'https://api.example.com',
  applicationSlug: 'my-app',
});

try {
  // Use loginWithMfaSupport for MFA-aware flow
  const result = await manager.loginWithMfaSupport(emailPasswordStrategy, {
    email: '[email protected]',
    password: 'password123'
  });
  
  if (isMfaChallenge(result)) {
    // MFA required - prompt user for code
    console.log('MFA required, method:', result.mfaMethod);
    
    // Later, verify the MFA code
    await manager.verifyMfa({
      challengeId: result.challengeId,
      sessionId: result.sessionId,
      code: '123456' // User-entered code
    });
  } else {
    // Login successful without MFA
    console.log('Logged in!', result.user);
  }
} catch (error) {
  if (error instanceof MfaRequiredError) {
    // Handle MFA requirement
  }
}

Configuration

AuthClientConfig

interface AuthClientConfig {
  /** Base URL for the auth API server */
  baseUrl: string;
  
  /** Application slug for multi-tenant support */
  applicationSlug: string;
  
  /** Optional storage configuration */
  storage?: {
    key: string; // Storage key prefix
  };
  
  /** Optional endpoint overrides */
  endpoints?: Partial<EndpointConfig>;
  
  /** Optional default device info */
  defaultDevice?: DeviceInfo;
  
  /** Request timeout in milliseconds (default: 10000) */
  timeout?: number;
  
  /** Enable debug logging */
  debug?: boolean;
}

Default Endpoints

const DEFAULT_ENDPOINTS = {
  login: '/auth/login',
  register: '/auth/register',
  logout: '/auth/logout',
  logoutAll: '/auth/logout/all',
  logoutOthers: '/auth/logout/others',
  refresh: '/auth/refresh',
  mfaVerify: '/auth/mfa/verify',
  verifyEmail: '/auth/verify-email',
  resendVerification: '/auth/resend-verification',
  me: '/auth/me'
};

API Reference

AuthManager

The main class for managing authentication.

Methods

| Method | Description | |--------|-------------| | login(strategy, payload) | Login using an authentication strategy | | loginWithMfaSupport(strategy, payload) | Login with MFA handling | | verifyMfa(payload) | Verify MFA code | | logout() | Logout current session | | logoutAll() | Logout from all sessions | | logoutOthers() | Logout from other sessions | | refreshToken() | Manually refresh the access token | | getUser() | Get current user | | getSession() | Get current session | | getAccessToken() | Get current access token | | isAuthenticated() | Check if user is authenticated | | subscribe(listener) | Subscribe to state changes |

EmailPasswordStrategy

Strategy for email/password authentication.

const strategy = new EmailPasswordStrategy(
  'my-app',        // applicationSlug
  defaultDevice?   // optional device info
);

await auth.login(strategy, {
  email: '[email protected]',
  password: 'password123',
  device?: { /* optional device override */ }
});

Error Handling

All authentication errors extend AuthError:

import { 
  AuthError,
  AuthErrorCode,
  InvalidCredentialsError,
  MfaRequiredError,
  SessionExpiredError,
  NetworkError,
  RateLimitError
} from 'auth-client';

try {
  await auth.login(strategy, credentials);
} catch (error) {
  if (error instanceof InvalidCredentialsError) {
    // Handle invalid credentials
  } else if (error instanceof MfaRequiredError) {
    // Handle MFA requirement
  } else if (error instanceof RateLimitError) {
    // Handle rate limiting
    console.log('Retry after:', error.retryAfter);
  } else if (error instanceof AuthError) {
    // Handle other auth errors
    console.log('Error code:', error.code);
    console.log('Is retryable:', error.isRetryable());
  }
}

Error Codes

| Code | Description | |------|-------------| | AUTH_1001 | Invalid credentials | | AUTH_1002 | Account locked | | AUTH_1003 | Account disabled | | AUTH_1004 | Email not verified | | AUTH_1009 | Session expired | | MFA_2001 | MFA required | | MFA_2002 | Invalid MFA code | | REG_3001 | Email already exists | | REG_3002 | Weak password | | RATE_7001 | Rate limit exceeded |

TypeScript

The package is fully typed. Key types are exported:

import type {
  User,
  Session,
  AuthTokens,
  AuthSnapshot,
  AuthEventType,
  LoginResult,
  AuthenticatedResult,
  MfaChallengeResult,
  DeviceInfo,
  AuthClientConfig,
} from 'auth-client';

License

MIT