Skip to content

DurjoyGH/Smart_Auth_SDK

Repository files navigation

Smart Auth SDK

Production-Grade Authentication Lifecycle Management

A complete auth ecosystem for modern JavaScript & TypeScript applications.

npm version CI License TypeScript Tree Shakeable

Features β€’ Quick Start β€’ Packages β€’ Architecture β€’ Security β€’ Contributing


The Problem

Authentication is never "just a JWT". Every real-world app ends up writing the same boilerplate:

  • Token refresh logic with race conditions
  • 401 interceptors that queue and retry requests
  • Multi-tab session synchronization
  • Secure storage with SSR compatibility
  • Protected route wrappers
  • Role-based access control

smart-auth-sdk solves all of this in one modular, type-safe, framework-agnostic SDK.

✨ Features

Feature Description
πŸ” JWT Lifecycle Automatic token management, expiration detection, proactive refresh
πŸ”„ Silent Refresh Concurrent-safe token refresh with request queuing β€” only one refresh call, ever
πŸ“± Multi-Tab Sync BroadcastChannel + storage event fallback β€” logout in one tab, logout everywhere
βš›οΈ React Integration Provider, hooks (useAuth, useUser, usePermissions), route guards
πŸ›‘οΈ Express Middleware JWT verification, RBAC authorization, token issuance, refresh rotation
🌐 HTTP Interceptors Axios and fetch wrappers with automatic token attachment and 401 retry
πŸ’Ύ Storage Adapters Memory (default), localStorage, sessionStorage, cookies, or custom
πŸ”’ Security-First httpOnly cookies, token rotation, clock skew handling, sensitive data masking
πŸ“¦ Modular Install only what you need β€” tree-shakeable, zero cross-package bloat
🧩 TypeScript Full type safety with generic user types and strict interfaces

πŸ“¦ Packages

Package Description Size
@smart-auth/core Auth engine, token manager, storage adapters, multi-tab sync core size
@smart-auth/react React Provider, hooks, ProtectedRoute, GuestRoute react size
@smart-auth/axios Axios interceptors with concurrent refresh queue axios size
@smart-auth/fetch Enhanced fetch wrapper with auto-refresh fetch size
@smart-auth/express Express middleware β€” JWT verify, RBAC, token issuance express size
@smart-auth/types Shared TypeScript interfaces and error classes Types only
@smart-auth/shared Internal utilities β€” JWT decode, event emitter, logger Internal

πŸš€ Quick Start

Installation

# Core (required)
npm install @smart-auth/core

# React integration
npm install @smart-auth/react

# HTTP interceptors (pick one or both)
npm install @smart-auth/axios    # for Axios
npm install @smart-auth/fetch    # for fetch API

# Express backend
npm install @smart-auth/express

React App

import { AuthProvider, useAuth, ProtectedRoute } from '@smart-auth/react';

function App() {
  return (
    <AuthProvider
      config={{
        apiBaseUrl: '/api',
        refresh: { endpoint: '/auth/refresh' },
        autoRefresh: true,
      }}
    >
      <Router>
        <Route
          path="/dashboard"
          element={
            <ProtectedRoute fallback={<Navigate to="/login" />}>
              <Dashboard />
            </ProtectedRoute>
          }
        />
      </Router>
    </AuthProvider>
  );
}

function Dashboard() {
  const { user, logout, loading } = useAuth();

  if (loading) return <Spinner />;

  return (
    <div>
      <h1>Welcome, {user?.name}</h1>
      <button onClick={() => logout()}>Sign Out</button>
    </div>
  );
}

Express API

import express from 'express';
import {
  createTokenIssuer,
  verifyAccessToken,
  authorize,
  createRefreshHandler,
} from '@smart-auth/express';

const app = express();

// Create a token issuer
const issuer = createTokenIssuer({
  accessTokenSecret: process.env.JWT_ACCESS_SECRET!,
  refreshTokenSecret: process.env.JWT_REFRESH_SECRET!,
  accessTokenExpiry: '15m',
  refreshTokenExpiry: '7d',
});

// Login endpoint
app.post('/auth/login', async (req, res) => {
  const user = await authenticate(req.body); // your logic
  const tokens = issuer.issueTokenPair({
    sub: user.id,
    email: user.email,
    roles: user.roles,
  });
  res.json({ user, ...tokens });
});

// Protect any route with one middleware
app.get(
  '/api/profile',
  verifyAccessToken({ secret: process.env.JWT_ACCESS_SECRET! }),
  (req, res) => {
    res.json({ userId: (req as any).auth.userId });
  },
);

// Role-based access control
app.delete(
  '/api/users/:id',
  verifyAccessToken({ secret: process.env.JWT_ACCESS_SECRET! }),
  authorize(['admin']),
  (req, res) => {
    // Only admins reach here
  },
);

// Refresh token rotation
const refreshHandler = createRefreshHandler({ issuer });
app.post('/auth/refresh', refreshHandler.middleware);

Axios Interceptors

import axios from 'axios';
import { createAuth } from '@smart-auth/core';
import { createSmartAxios } from '@smart-auth/axios';

const auth = createAuth({
  apiBaseUrl: '/api',
  refresh: { endpoint: '/auth/refresh' },
});

const api = axios.create({ baseURL: '/api' });

// Attach interceptors β€” tokens are injected automatically
const cleanup = createSmartAxios({ auth, axios: api });

// Every request is now authenticated. 401s trigger silent refresh.
const { data } = await api.get('/protected-resource');

Fetch Wrapper

import { createAuth } from '@smart-auth/core';
import { createSmartFetch } from '@smart-auth/fetch';

const auth = createAuth({ apiBaseUrl: '/api' });
const smartFetch = createSmartFetch({ auth, baseUrl: '/api' });

// Automatic token injection + 401 refresh retry
const response = await smartFetch('/protected-resource');
const data = await response.json();

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Your Application                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   React    β”‚   Axios    β”‚   Fetch    β”‚     Express       β”‚
β”‚   Hooks &  β”‚ Interceptorβ”‚  Wrapper   β”‚    Middleware      β”‚
β”‚   Guards   β”‚  + Queue   β”‚            β”‚    + RBAC          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚           @smart-auth/core           β”‚                   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚                   β”‚
β”‚  β”‚         Auth Engine            β”‚  β”‚                   β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚   @smart-auth/    β”‚
β”‚  β”‚  β”‚   Token    β”‚   Refresh   β”‚  β”‚  β”‚     express       β”‚
β”‚  β”‚  β”‚  Manager   β”‚   Manager   β”‚  β”‚  β”‚                   β”‚
β”‚  β”‚  β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€  β”‚  β”‚  Token Issuer     β”‚
β”‚  β”‚  β”‚  Session   β”‚  Multi-Tab  β”‚  β”‚  β”‚  JWT Verify       β”‚
β”‚  β”‚  β”‚  Manager   β”‚    Sync     β”‚  β”‚  β”‚  RBAC Authorize   β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚  Cookie Helpers   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚          @smart-auth/shared          β”‚                   β”‚
β”‚    JWT Decode Β· Events Β· Logger      β”‚                   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                   @smart-auth/types                      β”‚
β”‚         Interfaces Β· Errors Β· Config Types               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Note: @smart-auth/express does not depend on @smart-auth/core. The backend package shares only types and utilities β€” no browser code leaks into your server bundle.

πŸ”’ Security

Practice Implementation
Default to memory storage Access tokens are stored in-memory by default (XSS-resistant)
httpOnly cookies Refresh tokens should use httpOnly cookies set by the server
Token rotation Old refresh tokens are invalidated on each refresh
Clock skew tolerance Configurable buffer prevents premature token rejection
Data masking Sensitive data (tokens, secrets) is masked in all log output
CSRF protection SameSite cookie attribute, configurable per environment
Concurrent refresh safety Only one refresh request fires, all others queue and await

πŸ§ͺ Testing

# Run all unit tests (58 tests across 3 packages)
pnpm test

# Run the fullstack integration demo
cd examples/fullstack-demo && pnpm dev

🀝 Contributing

Contributions are welcome! This is a pnpm monorepo managed with Turborepo.

# Clone and install
git clone https://github.com/DurjoyGH/Smart_Auth_SDK.git
cd Smart_Auth_SDK
pnpm install

# Build all packages
pnpm build

# Run tests
pnpm test

# Run the demo
cd examples/fullstack-demo && pnpm dev

Project Structure

smart-auth-sdk/
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ types/          # Shared interfaces and error classes
β”‚   β”œβ”€β”€ shared/         # Internal utilities (JWT, events, logger)
β”‚   β”œβ”€β”€ core/           # Auth engine (browser + SSR)
β”‚   β”œβ”€β”€ react/          # React Provider, hooks, route guards
β”‚   β”œβ”€β”€ axios/          # Axios interceptors
β”‚   β”œβ”€β”€ fetch/          # Fetch wrapper
β”‚   └── express/        # Express middleware (server-only)
β”œβ”€β”€ examples/
β”‚   └── fullstack-demo/ # Working Express + frontend demo
β”œβ”€β”€ docs/               # Guides and API documentation
└── .github/workflows/  # CI/CD with GitHub Actions

πŸ“„ License

MIT Β© Durjoy Ghosh

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors