A simple yet powerful authentication boilerplate for building MERN stack applications. This package streamlines the process of adding authentication, authorization, and basic CRUD operations to your Express.js backend, along with CLI-based project initialization.
- User Signup
- User Login with JWT (JSON Web Tokens)
- Password Hashing using bcrypt
- Role-Based Access Control (RBAC)
- Middleware to restrict access based on user roles
- Pre-configured CRUD routes for any Mongoose model
- Easy-to-use, extendable, and RESTful
- Nodemailer setup for sending emails (e.g., password reset, email verification)
- JWT Authentication Middleware for protecting routes
- Quickly generate folder structures and boilerplate code for your Express.js backend with a single command.
Install the package using npm:
npm install auth-boilerplate-express
const express = require("express");
const mongoose = require("mongoose");
const { Auth, CRUD, authenticateToken, verifyRole } = require("auth-boilerplate-express");
const app = express();
const PORT = process.env.PORT || 5000;
const SECRET_KEY = "your_secret_key"; // Replace with your actual secret key
// Example Mongoose model
const UserModel = mongoose.model("User", new mongoose.Schema({
username: String,
email: String,
password: String,
role: { type: String, default: "user" },
}));
// Middleware
app.use(express.json());
// Authentication Routes
app.use("/auth", Auth(UserModel, SECRET_KEY));
// CRUD Routes (for any model)
const SomeModel = mongoose.model("SomeModel", new mongoose.Schema({ title: String, description: String }));
app.use("/api", CRUD(SomeModel));
// Protected Route Example
app.get(
"/protected",
authenticateToken(SECRET_KEY),
verifyRole("admin"),
(req, res) => {
res.status(200).send("Welcome Admin!");
}
);
// Start Server
mongoose
.connect("your_database_uri", { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => app.listen(PORT, () => console.log(`Server running on port ${PORT}`)))
.catch((err) => console.log(err));
Run the following command to generate the boilerplate structure:
npx init-backend <project-name>
This will create the following structure:
<project-name>/
|-- config/
|-- controllers/
|-- models/
|-- routes/
|-- middleware/
|-- utils/
|-- .env
|-- .gitignore
|-- index.js
POST /auth/signup
Body:
{
"username": "testuser",
"email": "test@example.com",
"password": "securepassword"
}
Response:
{
"message": "User created successfully!"
}
POST /auth/login
Body:
{
"email": "test@example.com",
"password": "securepassword"
}
Response:
{
"message": "Login successful!",
"token": "<JWT_TOKEN>"
}
POST /api
GET /api
GET /api/:id
PUT /api/:id
DELETE /api/:id
GET /protected
Headers:
{
"Authorization": "Bearer <JWT_TOKEN>"
}
Add roles to your user model and use the verifyRole
middleware to restrict access to specific routes.
Example:
app.get("/admin", authenticateToken(SECRET_KEY), verifyRole("admin"), (req, res) => {
res.status(200).send("Welcome Admin!");
});
Create a .env
file in your project root:
PORT=5000
DB_URI=your_database_uri
SECRET_KEY=your_secret_key
auth
jwt
mern
express
authentication
cli
role-based-access-control
crud
MIT License
Created by Abhishek Shrivastav. Feel free to reach out or contribute to this project!