A full-stack authentication system built with the MERN stack, featuring secure JWT-based login/registration with password complexity validation.
A production-ready authentication system implementing industry-standard security practices. Features user registration with enforced password complexity, email/password login with JWT token generation, and bcrypt password hashing. Built as a reusable auth module for MERN stack applications.
- User Registration -- Sign up with email, first name, last name, and password
- Password Complexity Enforcement -- Joi-based validation requiring minimum length, uppercase, lowercase, numbers, and symbols
- JWT Authentication -- Secure token generation with 7-day expiry
- Password Hashing -- Bcrypt-based password hashing for secure storage
- Protected Routes -- Token-verified access to protected resources
- Input Validation -- Server-side validation using Joi schemas
- CORS Enabled -- Cross-origin resource sharing for frontend communication
| Layer | Technology |
|---|---|
| Frontend | React, React Router, CSS Modules |
| Backend | Node.js, Express.js |
| Database | MongoDB, Mongoose ODM |
| Auth | JWT (jsonwebtoken), Bcrypt |
| Validation | Joi, joi-password-complexity |
| Config | dotenv, CORS |
Authentication-System/
├── client/ # React frontend
│ ├── public/
│ └── src/
│ ├── components/
│ │ ├── Login/ # Login form component
│ │ ├── Main/ # Main/dashboard component
│ │ └── Signup/ # Registration form component
│ ├── App.js
│ └── index.js
├── models/
│ └── user.js # Mongoose user schema with JWT generation
├── routes/
│ ├── auth.js # Login/authentication endpoints
│ └── users.js # User registration endpoints
├── db.js # MongoDB connection configuration
├── index.js # Express server entry point
├── .env # Environment variables (not committed)
└── package.json
- Node.js 16+
- MongoDB (local or Atlas)
- npm or yarn
# Clone the repository
git clone https://github.com/Sagargupta16/Authentication-System.git
cd Authentication-System
# Install backend dependencies
npm install
# Install frontend dependencies
cd client && npm install && cd ..Create a .env file in the root directory:
DB=mongodb://localhost:27017/auth-system # MongoDB connection string
JWTPRIVATEKEY=your_jwt_secret_key # JWT signing key
PORT=5000 # Server port
SALT=10 # Bcrypt salt rounds# Start both backend and frontend concurrently
npm run dev
# Or start separately:
npm start # Backend only (port 5000)
cd client && npm start # Frontend only (port 3000)| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/users |
Register a new user | No |
| POST | /api/auth |
Login and get JWT token | No |
POST /api/users
{
"firstName": "John",
"lastName": "Doe",
"email": "john@example.com",
"password": "SecurePass123!"
}POST /api/auth
{
"email": "john@example.com",
"password": "SecurePass123!"
}Response:
{
"data": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"message": "Logged in successfully"
}- Passwords are hashed with bcrypt before storage
- JWT tokens expire after 7 days
- Password complexity enforced: min 8 chars, uppercase, lowercase, numbers, symbols
- Input sanitization via Joi validation schemas
- CORS configured for frontend origin
MIT