@mondalrakesh461/express-jwt-auth-boilerplate v1.0.0
express-jwt-auth-boilerplate A CLI tool to generate a Node.js, Express, and MongoDB authentication boilerplate with JWT. Overview express-jwt-auth-boilerplate is a command-line tool that scaffolds a ready-to-use authentication system for Node.js applications. It generates a project structure with user signup and signin endpoints, JWT authentication, and MongoDB integration. The boilerplate includes Express server setup, Mongoose models, and middleware for token validation. Installation
Usage
Create a new project directory and initialize a package.json: mkdir my-project cd my-project npm init -y
Install it locally in a project: npm install express-jwt-auth-boilerplate
Generate the boilerplate: Run the init command to create the authentication boilerplate: npx express-jwt-auth-boilerplate init
This generates:
Folders: controllers, middlewares, models, routes Files: userController.js, validateToken.js, userModel.js, userRoutes.js, app.js Dependencies: express, mongoose, bcrypt, jsonwebtoken, cors, dotenv, lodash
Set up environment variables:Create a .env file in the project root: JWT_SECRET=your_jwt_secret_here MONGO_URI=your_mongodb_connection_string_here PORT=3000
Replace your_jwt_secret_here with a secure key and your_mongodb_connection_string_here with your MongoDB URI (e.g., from MongoDB Atlas).
Add a start script to package.json: "scripts": { "dev": "node --watch app.js" }
Run the server: npm start
The server runs on http://localhost:3000 (or the port specified in .env).
API Endpoints
Sign Up: POST /api/users/signup Content-Type: application/json
{ "firstName": "John", "lastName": "Doe", "email": "john@example.com", "password": "password123" }
Sign In: POST /api/users/signin Content-Type: application/json
{ "email": "john@example.com", "password": "password123" }
Project Structure The generated boilerplate includes:
controllers/: Handles signup and signin logic (userController.js). middlewares/: JWT authentication middleware (validateToken.js). models/: Mongoose user schema (userModel.js). routes/: API routes (userRoutes.js). app.js: Express server setup with MongoDB connection.
Requirements
Node.js (v14 or higher) MongoDB (local or cloud, e.g., MongoDB Atlas) npm
1 year ago