How burakorkmez/mern-advanced-auth works
Project Type and Purpose
• This is a full-stack authentication tutorial application.
• Frontend: React SPA (Vite + Tailwind CSS) implementing signup, login, email verification, forgot/reset password, and a protected dashboard UI.
• Backend: Node.js + Express REST API with MongoDB (via Mongoose) for user data, JWT for authentication, and Mailtrap for email delivery.
High-Level File Structure Analysis
• Top-level “frontend” directory:
• Vite config, PostCSS/Tailwind config, ESLint.
• src/ contains React components, pages, global styles, and a lightweight store for auth state.
• public/ holds static assets (HTML shell, screenshot for README).
• Top-level “backend” directory:
• controllers/: business logic for auth endpoints.
• routes/: Express routes wiring controllers.
• models/: Mongoose schema for User.
• middleware/: JWT verification.
• db/: MongoDB connection setup.
• mailtrap/: email templates and Mailtrap configuration.
• utils/: token generation and cookie-setting helper.
• index.js: Express app bootstrap and route mounting.
Core Architectural Components
a. Frontend SPA
• React components and pages (Login, Signup, Dashboard, etc.)
• AuthStore: client-side state management for user session
• HTTP client (fetch or Axios) calls to backend endpoints
b. Backend API
• Express server handling REST endpoints: signup, login, verify email, forgot/reset password, checkAuth, logout
• JWT middleware enforces route protection
• Controllers coordinate DB operations and email triggers
c. Database
• MongoDB (hosted externally or locally via MONGO_URI)
• Mongoose for ODM mapping and schema validation
d. Email Service
• Mailtrap API for sending verification, reset, and welcome emails via configured templates
Relationships and Data Flows
1. Browser (React SPA) → HTTP POST /signup → Express → Controller → Mongoose writes User → generate JWT → set HTTP-Only cookie → send verification email via Mailtrap → respond to client
2. Browser follows email link → HTTP GET /verify-email → Express → Controller marks user as verified
3. Protected pages (e.g., Dashboard) → HTTP GET /checkAuth → JWT middleware validates token → allows access
4. Forgot/Reset flows: browser triggers POST /forgot-password → email sent; POST /reset-password → controller updates password
5. Logout: clears cookie
Architectural Patterns and Principles
• MVC-style layering in backend (Routes → Controllers → Models → DB)
• Stateless JWT authentication with HTTP-Only cookies for XSS protection
• Single responsibility per module (e.g., dedicated utils for token generation, dedicated mailtrap folder)
• Clear separation of concerns between frontend UI and backend API
Technologies & Frameworks
• Frontend: React, Vite, Tailwind CSS, PostCSS, ESLint, Zustand (or similar store)
• Backend: Node.js, Express, Mongoose (MongoDB), JSON Web Tokens, dotenv for config
• Email: Mailtrap API
• Deployment: npm run build (frontend Vite build), npm run start (backend server)