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

@keshavasilva/just-auth

v1.0.4

Published

A lightweight, headless authentication package for React and Next.js applications

Downloads

18

Readme

@keshavasilva/just-auth

A lightweight, headless authentication package for React and Next.js applications with automatic token refresh and flexible storage strategies.

✨ Features

  • 🔐 Headless Authentication - No UI components, just logic
  • 🔄 Automatic Token Refresh - Seamless token renewal on expiry
  • 🏪 Flexible Storage - localStorage, cookies, or custom storage strategies
  • 🚫 Request Queuing - Prevents race conditions during token refresh
  • 📱 SSR Compatible - Works with Next.js server-side rendering
  • 🎯 TypeScript First - Full type safety out of the box
  • ⚡ Zero Dependencies - Only requires React as peer dependency
  • 🔧 Configurable - Customize URLs, storage, and error handling

📦 Installation

npm install @keshavasilva/just-auth

🚀 Quick Start

1. Wrap your app with AuthProvider

import { AuthProvider } from '@keshavasilva/just-auth';

function App() {
  return (
    <AuthProvider
      loginUrl="/api/auth/login"
      refreshUrl="/api/auth/refresh"
      onAuthError={(error) => {
        console.log('Authentication error:', error);
        // Redirect to login page
      }}
    >
      <YourAppComponents />
    </AuthProvider>
  );
}

2. Use the authentication hook

import { useAuth } from '@keshavasilva/just-auth';

function LoginPage() {
  const { login, loading, error } = useAuth();

  const handleLogin = async () => {
    try {
      await login({ email: '[email protected]', password: 'password' });
      // User is now logged in, tokens are stored automatically
    } catch (err) {
      console.error('Login failed:', err);
    }
  };

  return (
    <div>
      <button onClick={handleLogin} disabled={loading}>
        {loading ? 'Logging in...' : 'Login'}
      </button>
      {error && <p>Error: {error.message}</p>}
    </div>
  );
}

3. Access user data and authentication state

import { useAuth } from '@keshavasilva/just-auth';

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

  if (!isAuthenticated) {
    return <div>Please log in</div>;
  }

  return (
    <div>
      <h1>Welcome, {user?.name}!</h1>
      <button onClick={logout}>Logout</button>
      
      {/* Access token for manual API calls */}
      <button onClick={() => {
        const token = getAccessToken();
        console.log('Current token:', token);
      }}>
        Get Token
      </button>
    </div>
  );
}

🔧 API Reference

AuthProvider Props

| Prop | Type | Required | Description | |------|------|----------|-------------| | loginUrl | string | ✅ | Endpoint for user login | | refreshUrl | string | ✅ | Endpoint for token refresh | | storageStrategy | StorageStrategy | ❌ | Custom storage implementation (defaults to localStorage) | | onAuthError | (error: Error) => void | ❌ | Callback when authentication fails | | children | ReactNode | ✅ | Your app components |

useAuth Hook Returns

| Property | Type | Description | |----------|------|-------------| | user | object \| null | Current user data | | isAuthenticated | boolean | Whether user is logged in | | loading | boolean | Loading state for auth operations | | error | Error \| null | Current error state | | login(payload) | (payload: object) => Promise<void> | Login function | | logout() | () => void | Logout function | | getAccessToken() | () => string \| null | Get current access token |

🏪 Storage Strategies

Default (localStorage)

<AuthProvider
  loginUrl="/api/auth/login"
  refreshUrl="/api/auth/refresh"
  // Uses localStorage by default
/>

Custom Storage (e.g., Cookies for SSR)

import Cookies from 'js-cookie';

const cookieStorage = {
  get: (key: string) => Cookies.get(key) || null,
  set: (key: string, value: string) => Cookies.set(key, value),
  clear: (key: string) => Cookies.remove(key),
};

<AuthProvider
  loginUrl="/api/auth/login"
  refreshUrl="/api/auth/refresh"
  storageStrategy={cookieStorage}
/>

Session Storage

const sessionStorage = {
  get: (key: string) => window.sessionStorage.getItem(key),
  set: (key: string, value: string) => window.sessionStorage.setItem(key, value),
  clear: (key: string) => window.sessionStorage.removeItem(key),
};

<AuthProvider
  storageStrategy={sessionStorage}
  // ... other props
/>

🔄 Automatic Token Refresh

The package automatically handles token refresh when:

  • ✅ API returns 401 - Automatically refreshes tokens and retries request
  • ✅ Multiple simultaneous requests - Queues requests to prevent race conditions
  • ✅ Transparent to user - Original request succeeds after refresh
  • ✅ Graceful failure - Calls onAuthError if refresh fails
// This request might trigger automatic token refresh
const response = await fetch('/api/protected-data', {
  headers: {
    'Authorization': `Bearer ${getAccessToken()}`
  }
});
// User never sees 401 error - refresh happens automatically

🗄️ Backend API Requirements

Your backend should implement these endpoints:

Login Endpoint

// POST /api/auth/login
{
  "email": "[email protected]",
  "password": "password123"
}

// Response
{
  "accessToken": "eyJhbGciOiJIUzI1NiIs...",
  "refreshToken": "eyJhbGciOiJIUzI1NiIs...",
  "user": {
    "id": 1,
    "name": "John Doe",
    "email": "[email protected]"
  }
}

Refresh Endpoint

// POST /api/auth/refresh
{
  "refreshToken": "eyJhbGciOiJIUzI1NiIs..."
}

// Response
{
  "accessToken": "eyJhbGciOiJIUzI1NiIs...",
  "refreshToken": "eyJhbGciOiJIUzI1NiIs..." // optional new refresh token
}

🔐 Security Best Practices

  • ✅ Short-lived access tokens (15 minutes recommended)
  • ✅ Longer refresh tokens (7-30 days)
  • ✅ Secure HTTP-only cookies for refresh tokens (server-side)
  • ✅ HTTPS only in production
  • ✅ Token rotation on refresh
  • ✅ Logout endpoint to invalidate tokens

🌐 Next.js SSR Example

// pages/_app.tsx
import { AuthProvider } from '@keshavasilva/just-auth';
import Cookies from 'js-cookie';

const cookieStorage = {
  get: (key: string) => Cookies.get(key) || null,
  set: (key: string, value: string) => Cookies.set(key, value, { secure: true }),
  clear: (key: string) => Cookies.remove(key),
};

export default function App({ Component, pageProps }) {
  return (
    <AuthProvider
      loginUrl="/api/auth/login"
      refreshUrl="/api/auth/refresh"
      storageStrategy={cookieStorage}
      onAuthError={() => {
        window.location.href = '/login';
      }}
    >
      <Component {...pageProps} />
    </AuthProvider>
  );
}

🧪 Error Handling

import { useAuth } from '@keshavasilva/just-auth';

function MyComponent() {
  const { login, error } = useAuth();

  const handleLogin = async () => {
    try {
      await login({ email: '[email protected]', password: 'wrong' });
    } catch (err) {
      // Handle specific errors
      if (err.response?.status === 401) {
        alert('Invalid credentials');
      } else if (err.response?.status === 429) {
        alert('Too many attempts, try again later');
      } else {
        alert('Login failed');
      }
    }
  };

  return (
    <div>
      {error && <div className="error">{error.message}</div>}
      <button onClick={handleLogin}>Login</button>
    </div>
  );
}

📱 Token Types Supported

  • ✅ JWT (JSON Web Tokens) - Most common
  • ✅ Opaque tokens - Server-side validation
  • ✅ Session tokens - Traditional sessions
  • ✅ Custom token formats - Any Bearer token

🤝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

MIT © ASYNCHRO

🔗 Links


Made with ❤️ for the React community