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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-express-auth-kit

v1.1.5

Published

Lightweight authentication state manager for React + Express

Downloads

612

Readme

react-express-auth-kit

A lightweight, flexible authentication state manager for React applications. Works seamlessly with Express.js or any REST backend.

Built with TypeScript, but fully compatible with JavaScript projects.

react-express-auth-kit focuses on frontend authentication state management — handling user state, login, logout, and persistence — without locking you into any specific backend architecture.


🚀 Features

  • Simple and reusable AuthProvider
  • Manages authentication state (user, loading)
  • Built-in login & logout helpers
  • Works with any backend (Express, Nest, etc.)
  • Persistent auth state using localStorage
  • Token handling via cookies
  • Fully typed with TypeScript
  • JavaScript friendly
  • Lightweight and dependency-free
  • Supports ESM and CommonJS

📦 Installation

npm install react-express-auth-kit

or

yarn add react-express-auth-kit

🛠 Basic Usage

Wrap your application with AuthProvider.

import { AuthProvider } from "react-express-auth-kit";
import App from "./App";

export default function Root() {
  return (
    <AuthProvider>
      <App />
    </AuthProvider>
  );
}

⚙️ Configuring Routes (Optional)

By default, react-express-auth-kit uses:

  • Login/auth/login
  • Logout/auth/logout

You can customize them if needed:

<AuthProvider
  config={{
    loginRoute: "http://localhost:5000/auth/login",
    logoutRoute: "http://localhost:5000/auth/logout",
  }}
>
  <App />
</AuthProvider>

🔐 Logging In

Use the login function from useAuth().

import { useAuth } from "react-express-auth-kit";

function LoginForm() {
  const { login } = useAuth();

  const handleLogin = async () => {
    try {
      const user = await login("[email protected]", "password123");
      console.log("Logged in user:", user);
    } catch (err) {
      console.error("Login failed");
    }
  };

  return <button onClick={handleLogin}>Login</button>;
}

Expected Backend Response

Your backend must return the following structure:

{
  "user": {
    "id": "123",
    "name": "John Doe",
    "email": "[email protected]"
  },
  "token": "JWT_TOKEN_HERE"
}

📌 On successful login:

  • user → stored in localStorage (authkit-user)
  • token → stored in cookies (accessToken)

🎣 Accessing Auth State

import { useAuth } from "react-express-auth-kit";

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

  if (loading) return <p>Loading...</p>;
  if (!user) return <p>Please login</p>;

  return (
    <div>
      <h2>Welcome, {user.name}</h2>
      <button onClick={logout}>Logout</button>
    </div>
  );
}

🚪 Logging Out

logout();

What happens on logout:

  • Clears user state
  • Removes authkit-user from localStorage
  • Removes accessToken cookie
  • Optionally calls backend logout API

🔧 Custom Logout API (Optional)

<AuthProvider
  config={{
    logoutApi: async () => {
      await fetch("/api/logout", { method: "POST" });
    },
  }}
>
  <App />
</AuthProvider>

🧩 What react-express-auth-kit Stores

| Data | Location | Key Name | | ---- | -------------- | -------------- | | User | localStorage | authkit-user | | JWT | Browser Cookie | accessToken |


🏗 TypeScript Support

Full TypeScript support included:

import {
  AuthProvider,
  useAuth,
  type AuthKitConfig,
} from "react-express-auth-kit";

🧠 What This Package Does NOT Do

  • ❌ Token refresh
  • ❌ Role/permission handling
  • ❌ Backend authentication logic
  • ❌ OAuth / Social login

These are intentionally left to keep the package lightweight.


📤 Contributing

Pull requests are welcome. Please open an issue first to discuss major changes.


📄 License

MIT License © 2025 Developed by Meheraj Hosen