A complete backend authentication system built with Node.js, ExpressJS, MongoDB, and Mongoose.
It demonstrates secure user registration, email verification, login, JWT access and refresh tokens, HttpOnly cookies, protected routes, logout, forgot password, and password reset flows.
- User registration
- Password hashing with bcryptjs
- Email verification flow
- Resend verification flow
- Login with email and password
- JWT access tokens
- JWT refresh tokens
- HttpOnly authentication cookies
- Protected profile route
- Refresh access token flow
- Logout and session invalidation
- Forgot password flow
- Password reset flow
- Refresh token hashing
- Verification token hashing
- Password reset token hashing
- MongoDB integration
- Mongoose schema validation
- Request validation
- Centralized error handling
- Graceful shutdown
- Environment configuration
- Health check endpoint
- API testing with
requests.http
- Node.js
- ExpressJS
- MongoDB
- Mongoose
- bcryptjs
- JSON Web Token
- cookie-parser
- Nodemon
GET /GET /api/v1/healthPOST /api/v1/auth/registerPOST /api/v1/auth/verify-emailPOST /api/v1/auth/resend-verificationPOST /api/v1/auth/loginPOST /api/v1/auth/refreshPOST /api/v1/auth/logoutGET /api/v1/auth/profilePOST /api/v1/auth/forgot-passwordPOST /api/v1/auth/reset-passwordCreate a .env file using .env.example:
PORT=1198
NODE_ENV=development
MONGODB_URI=mongodb://127.0.0.1:27017/authentication_system
JWT_ACCESS_SECRET=replace_with_a_strong_access_secret
JWT_REFRESH_SECRET=replace_with_a_strong_refresh_secret
JWT_ACCESS_EXPIRES_IN=15m
JWT_REFRESH_EXPIRES_IN=7d
ACCESS_COOKIE_MAX_AGE=900000
REFRESH_COOKIE_MAX_AGE=604800000The .env file is ignored by Git. .env.example documents the required environment variables.
npm installnpm run devnpm start1198
Register
|
v
Verify Email
|
v
Login
|
v
Access Token + Refresh Token Cookies
|
v
Protected Routes
|
v
Refresh Access Token
|
v
Logout
Forgot Password
|
v
Generate Reset Token
|
v
Validate Reset Token
|
v
Set New Password
|
v
Invalidate Existing Refresh Session
authentication-system/
├── src/
│ ├── config/
│ │ └── database.js
│ ├── controllers/
│ │ └── auth.controllers.js
│ ├── middlewares/
│ │ ├── auth.middleware.js
│ │ ├── errorHandler.middleware.js
│ │ └── notFound.middleware.js
│ ├── models/
│ │ └── user.model.js
│ ├── routes/
│ │ └── auth.routes.js
│ ├── utils/
│ │ ├── cookie.utils.js
│ │ └── token.utils.js
│ ├── validators/
│ │ └── auth.validators.js
│ ├── app.js
│ └── server.js
├── .env.example
├── .gitignore
├── package.json
├── requests.http
└── README.md
- Passwords are hashed before storage.
- Raw refresh tokens are not stored in MongoDB.
- Verification and password-reset tokens are stored as hashes.
- Access and refresh tokens use separate secrets.
- Authentication cookies are HttpOnly.
- Protected routes validate access tokens before controller execution.
- Password reset invalidates the existing refresh session.
Detailed project and code documentation is available in DOCUMENTATION.md.
Ashish Ranjan
- Portfolio: https://www.ashishranjan.net
- GitHub: https://github.com/a2rp
- CodePen: https://codepen.io/ash1198
- LinkedIn: https://www.linkedin.com/in/aashishranjan
- Facebook: https://www.facebook.com/theash.ashish/
- YouTube: https://www.youtube.com/@ashishranjan-ashz?sub_confirmation=1
- Email: mailto:ash.ranjan09@gmail.com
- Support: https://a2rp-donation-page.netlify.app/
- Buy Me A Coffee: https://buymeacoffee.com/a2rp
- Patreon: https://patreon.com/a2rp
MIT License