Skip to content

mohankumaronly/Authentication_using_MERN

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

39 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” MERN Stack Authentication & Payments System

(Authentication Complete Β· Payments Backend Ready Β· Frontend Payments WIP)

A production-ready authentication system built with the MERN stack (MongoDB, Express, React, Node.js), extended with a manual UPI-based payment approval system for premium access.

This project focuses on secure, cookie-based authentication, Google OAuth, email workflows, and a backend-complete admin-controlled payment system.


πŸš€ Features


πŸ”‘ Authentication (βœ… Completed)

  • Local Authentication (Email + Password)
  • Google OAuth 2.0 Login
  • JWT Access & Refresh Tokens
  • HTTP-only cookie-based sessions
  • Silent token refresh
  • Secure logout & token revocation

πŸ“§ Email Workflows (βœ… Completed)

  • Email verification after registration
  • Forgot / Reset password flow
  • Token expiration handling
  • Email templates with Nodemailer

πŸ”’ Security (βœ… Completed)

  • HTTP-only, SameSite cookies (XSS safe)
  • Refresh token rotation
  • Rate limiting on auth routes
  • Helmet security headers
  • OAuth CSRF-safe state validation

🧠 Session Management (βœ… Completed)

  • /me endpoint for authenticated user
  • Persistent login across reloads
  • Auto logout on invalid refresh token

πŸ’³ Payments & Access Control (🚧 Backend Complete)

⚠️ Frontend payment UI is under development

Implemented (Backend)

  • Manual UPI QR-based payments
  • One-time & time-based plans
  • UTR submission & deduplication
  • Admin approval / rejection workflow
  • Secure admin-only routes
  • Access enforcement middleware
  • Premium route protection
  • Payment audit trail

Not Implemented Yet

  • Frontend payment UI
  • User payment dashboard
  • Admin payment UI (table)

πŸ‘‘ Admin Capabilities (βœ… Backend Complete)

  • List payments (filtered by status)
  • Approve / reject payments
  • Grant lifetime or time-based access
  • Admin-only protected routes

🧭 Tech Stack

Backend

  • Node.js + Express
  • MongoDB + Mongoose
  • JWT (Access & Refresh Tokens)
  • Google OAuth 2.0
  • Nodemailer
  • Helmet, CORS, Rate Limiting

Frontend

  • React (Vite)
  • React Router v6
  • Context API
  • Axios (with interceptors)
  • Tailwind CSS

πŸ” High-Level Authentication Flow

Login / OAuth
β†’ Access Token (15 min) stored in httpOnly cookie
β†’ Refresh Token stored in DB + cookie
β†’ Access expires
β†’ Silent refresh (/refresh-token)
β†’ Retry original request

πŸ› οΈ API Endpoints

Auth Routes

Method Endpoint Description
POST /api/auth/register Register & send verification email
POST /api/auth/login Login (rate limited)
GET /api/auth/google Start Google OAuth
GET /api/auth/google/callback OAuth callback
POST /api/auth/verify-email/:token Verify email
POST /api/auth/forgot-password Send reset email
POST /api/auth/reset-password/:token Reset password
POST /api/auth/refresh-token Refresh access token
POST /api/auth/logout Logout & revoke refresh token
GET /api/auth/me Get authenticated user

Payment Routes (Backend Ready)

Method Endpoint Description
POST /api/payment/intent Generate UPI QR intent
POST /api/payment/verify Submit UTR
GET /api/payment/admin/payments List all payments (admin)
GET /api/payment/admin/payments?status=PENDING List pending payments
POST /api/payment/admin/payments/:id/approve Approve payment
POST /api/payment/admin/payments/:id/reject Reject payment

πŸ“‚ Project Structure (Updated)

Backend

backend
│── server.js
│── configuration/
β”‚   └── db.js
β”‚
β”œβ”€β”€ middlewares/
β”‚   β”œβ”€β”€ rate.limiter.js
β”‚   └── token.verification.js
β”‚
β”œβ”€β”€ modules/
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ routers/
β”‚   β”‚   └── validators/
β”‚   β”‚
β”‚   └── payment/
β”‚       β”œβ”€β”€ controllers/
β”‚       β”‚   β”œβ”€β”€ paymentIntent.controller.js
β”‚       β”‚   β”œβ”€β”€ paymentVerification.controller.js
β”‚       β”‚   └── adminPayment.controller.js
β”‚       β”œβ”€β”€ middleware/
β”‚       β”‚   β”œβ”€β”€ access.middleware.js
β”‚       β”‚   └── admin.middleware.js
β”‚       β”œβ”€β”€ models/
β”‚       β”‚   └── payment.model.js
β”‚       └── routers/
β”‚           └── payment.routes.js
β”‚
└── utils/
    └── Emails/

Frontend

frontend/src
│── context/
β”‚   └── AuthContext.jsx
β”‚
β”œβ”€β”€ components/
β”‚   β”œβ”€β”€ RequireAuth.jsx
β”‚   β”œβ”€β”€ RedirectIfAuth.jsx
β”‚   └── Loading.jsx
β”‚
β”œβ”€β”€ pages/
β”‚   β”œβ”€β”€ Auth/
β”‚   β”œβ”€β”€ Home/
β”‚   └── NotFound/
β”‚
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ api.js
β”‚   └── auth.service.js
β”‚
└── Routers/
    └── AppRouters.jsx

🚧 Roadmap (Planned Enhancements)

1️⃣ Pagination for Admin Payments

/admin/payments?page=1&limit=10

2️⃣ Reject Reason Support

  • Admin can provide reason
  • User can view rejection message

3️⃣ Admin Payment UI

  • Table view
  • Approve / Reject buttons
  • Status filters

4️⃣ Time-Based Access Auto Expiry

  • Middleware / cron job
  • Disable access after endDate

5️⃣ Cleanup & Hardening

  • DB indexes (status, userId)
  • Validation tightening
  • RBAC upgrade path

🏁 Production Notes

  • Auth system fully integrated with frontend
  • Payment backend is stable & secure
  • Frontend payment integration is in progress
  • Manual approval avoids unsafe automation
  • Ready for Docker & cloud deployment

πŸ“œ License

MIT License

About

Authentication using the MERN stack with the docker compose

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages