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

gagan-auth-kit

v1.0.0

Published

A lightweight, reusable authentication module for Express.js with JWT, bcryptjs password hashing, and MongoDB support

Readme

🔐 Gagan Auth Kit

A lightweight, reusable authentication module for Express.js applications with JWT-based authentication, password hashing with bcryptjs, and MongoDB support.

✨ Features

  • User Registration & Login - Pre-built routes for user authentication
  • JWT Token Management - Secure token generation and validation
  • Password Hashing - Bcryptjs integration for secure password storage
  • Route Protection Middleware - Protect routes with protectRoute middleware
  • Mongoose Integration - Works seamlessly with MongoDB via Mongoose
  • Easy Integration - Simple initialization with minimal configuration

📦 Installation

npm install gagan-auth-kit

🚀 Quick Start

1. Initialize the Auth Module

const express = require('express');
const InitializeAuth = require('gagan-auth-kit');
const mongoose = require('mongoose');
const { userSchema } = require('./models/User'); // Your User model

const app = express();
app.use(express.json());

// Define your User model
const User = mongoose.model('User', userSchema);

// Initialize the auth kit
const { authRouter, protectRoute } = InitializeAuth({
  UserModel: User,
  jwtSecret: process.env.JWT_SECRET || 'your-secret-key'
});

// Use the auth router
app.use('/auth', authRouter);

// Example: Protect a route
app.get('/profile', protectRoute, (req, res) => {
  res.json({ message: 'This is a protected route', user: req.user });
});

app.listen(3000, () => console.log('Server running on port 3000'));

2. User Model Example

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model('User', userSchema);

📚 API Reference

InitializeAuth(options)

Initializes the authentication module with your configuration.

Parameters:

  • options.UserModel (required): Your Mongoose User model
  • options.jwtSecret (required): Secret key for JWT signing

Returns:

{
  authRouter: Express.Router,    // Router with /register and /login routes
  protectRoute: Function         // Middleware to protect routes
}

Routes

POST /register

Create a new user account.

Request Body:

{
  "name": "John Doe",
  "email": "[email protected]",
  "password": "securepassword123"
}

Response:

{
  "message": "User registered successfully",
  "user": {
    "_id": "...",
    "name": "John Doe",
    "email": "[email protected]"
  }
}

POST /login

Authenticate user and receive JWT token.

Request Body:

{
  "email": "[email protected]",
  "password": "securepassword123"
}

Response:

{
  "message": "Login successful",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "_id": "...",
    "name": "John Doe",
    "email": "[email protected]"
  }
}

Middleware: protectRoute

Protects routes by requiring a valid JWT token in the Authorization header.

Usage:

app.get('/protected-endpoint', protectRoute, (req, res) => {
  console.log(req.user); // Decoded JWT payload
  res.json({ message: 'Access granted', user: req.user });
});

Authorization Header Format:

Authorization: Bearer <your_jwt_token>

🔧 Environment Variables

Create a .env file in your project:

JWT_SECRET=your_very_secret_key_here
MONGODB_URI=mongodb://localhost:27017/your-db
NODE_ENV=development

📋 Dependencies

  • express - Web framework
  • mongoose - MongoDB object modeling
  • jsonwebtoken - JWT token generation and validation
  • bcryptjs - Password hashing
  • dotenv - Environment variable management

🤝 Error Handling

The module returns standard HTTP status codes:

  • 200 - Success
  • 201 - User created
  • 400 - Invalid input or user already exists
  • 401 - Unauthorized (invalid credentials or token)
  • 500 - Server error

🔐 Security Best Practices

  1. Never hardcode secrets - Always use environment variables
  2. Use HTTPS in production - Ensure secure token transmission
  3. Implement rate limiting - Prevent brute force attacks
  4. Set appropriate JWT expiration - Balance security and user experience
  5. Store tokens securely - Use httpOnly cookies on the client-side

📝 License

ISC

👨‍💻 Author

Gagan

🤝 Contributing

Feel free to fork and submit pull requests for any improvements.

📞 Support

For issues and questions, please open an issue on GitHub.