Skip to content

Favourof/mern-auth-system

Repository files navigation

MERN Authentication System

A production-ready authentication system built with MongoDB, Express, React, Node.js, and TypeScript.

TypeScript Node.js MongoDB Express

πŸš€ Features

Authentication

  • βœ… User registration with email verification
  • βœ… Secure login with JWT (Access + Refresh tokens)
  • βœ… Password reset via email
  • βœ… Change password (for logged-in users)
  • βœ… Session management with token rotation
  • βœ… True logout (token invalidation)

Security

  • βœ… Password hashing with bcrypt
  • βœ… httpOnly cookies for refresh tokens
  • βœ… Rate limiting on auth endpoints
  • βœ… Input validation and sanitization
  • βœ… Role-based access control (RBAC)
  • βœ… XSS protection
  • βœ… CORS configuration

Admin Features

  • βœ… User management dashboard
  • βœ… View all users
  • βœ… Update user roles
  • βœ… Delete users
  • βœ… User statistics

Email Features

  • βœ… Email verification
  • βœ… Welcome emails
  • βœ… Password reset emails
  • βœ… Resend verification option

πŸ“‹ Table of Contents

πŸ›  Tech Stack

Backend:

  • Node.js & Express.js
  • TypeScript
  • MongoDB & Mongoose
  • JWT (jsonwebtoken)
  • bcryptjs
  • Resend
  • express-rate-limit
  • express-validator
  • cookie-parser

Development:

  • ts-node-dev
  • TypeScript
  • ESLint (optional)
  • Prettier (optional)

πŸ“¦ Installation

Prerequisites

  • Node.js (v18 or higher)
  • MongoDB (local or Atlas)
  • npm or yarn

Setup

  1. Clone the repository
git clone https://github.com/Favourof/mern-auth-system
cd mern-auth-system
  1. Install dependencies
npm install
  1. Create .env file
cp .env.example .env
  1. Configure environment variables (see Environment Variables)

  2. Start development server

npm run dev

Server will run on http://localhost:5000

πŸ” Environment Variables

Create a .env file in the server directory:

# Server Configuration
PORT=
NODE_ENV=development

# Database
MONGO_URL=mongodb://localhost:27017/auth-db

# JWT Secrets (generate with: node -e "console.log(require('crypto').randomBytes(64).toString('hex'))")
JWT_SECRET=your_access_token_secret_here
JWT_REFRESH_SECRET=your_refresh_token_secret_here

# Client URL
CLIENT_URL=https://client-mu-ebon.vercel.app/

# Email Configuration
RESEND_API_KEY=
EMAIL_FROM=noreply@yourapp.com

Generate JWT Secrets

node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"

Run this twice to generate two different secrets.

πŸ“– API Documentation

Base URL

  • base_url: http://localhost:4002/api

πŸ§ͺ Testing

Manual Testing with Postman

  1. Import Collection
  2. Set Environment Variables
    • base_url: http://localhost:4002/api
    • access_token: (set after login)

Test Flow

# 1. Register
POST /api/auth/register

# 2. Check console for verification link

# 3. Verify email
POST /api/auth/verify-email

# 4. Login
POST /api/auth/login

# 5. Access protected route
GET /api/auth/me

# 6. Logout
POST /api/auth/logout

# 7. Refreash-token
POST /api/auth/refresh

# 8. Request Password Reset
POST /api/auth/forgot-password

# 9. Reset-password
POST /api/auth/reset-password

# 10. Change-password
GET /api/auth/change-password

# 11. Get All user
GET api/admin/users

# 12. Get user by id
GET api/admin/users/:id

# 13. Change User Role
PUT api/admin/users/:id

# 14. get user stats
GET api/admin/stats

# 15. Delete user by id

Delete api/admin/:id

Postman test Screenshot

  • Register with email login without verifying verify email verification token from emal resend verification email login after verifying welcome email Protested Route Refresh token log out Password Resent password reset with emali and newpassword Change password for login user making request without acesstoken get all user Get user by id change user role admin route with unathtorized user

πŸš€ Deployment

Deploy to Railway/Render

  1. Push to GitHub
git add .
git commit -m "Ready for deployment"
git push origin main
  1. Connect to Railway/Render
  • Import repository
  • Add environment variables
  • Deploy
  1. MongoDB Atlas Setup
  • Create cluster
  • Whitelist Railway/Render IPs (or allow all)
  • Update MONGO_URL in environment variables

Production Checklist

  • Set NODE_ENV=production
  • Use strong JWT secrets
  • Configure real email service (SendGrid, AWS SES)
  • Enable CORS for your frontend domain only
  • Set secure cookie options
  • Add monitoring (e.g., Sentry)
  • Enable HTTPS
  • Set up proper logging

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License.


πŸ‘€ Author

Your Name


πŸ™ Acknowledgments


πŸ“ž Support

For support, email favourtobiloba200@gmail.com or open an issue on GitHub.


Frontend url

https://client-mu-ebon.vercel.app/

⭐ If you find this project helpful, please give it a star!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors