How techinho/multi-auth-passportjs works
Project Type and Purpose
• This is a full-stack web application whose sole responsibility is user authentication.
• Built on Node.js/Express, it serves server-rendered EJS views and exposes JWT-protected REST endpoints.
• It integrates multiple OAuth providers (Google, GitHub) alongside traditional email/password login.
Main Components
• Client (Browser)
• EJS templates: home, login, register, profile
• LocalStorage (for JWT tokens)
• Web Server (Node.js/Express)
• app.js (entry point)
• Routes layer (auth-routes.js, profile-routes.js)
• Passport configuration (config/passport-setup.js)
• Controllers/middleware (in routes, for JWT issuance and session handling)
• Authentication Strategies
• Local strategy (email/password + bcrypt + JWT)
• OAuth strategies (Passport-Google, Passport-GitHub)
• Persistence
• MongoDB (via Mongoose user-model.js)
• Session store (connect-mongo for express-session)
• External Services
• Google OAuth 2.0 endpoints
• GitHub OAuth endpoints
Relationships and Data Flows
• User opens browser → requests home/login/register pages (GET).
• On form submit → Express routes invoke Passport local strategy → bcrypt password verification → JWT issued → token stored client-side.
• For OAuth: client directs user to Google/GitHub → external consent → callback to /auth/google/redirect or /auth/github/redirect → Passport strategy exchanges code → fetches profile → upserts user in MongoDB → issues JWT → redirects to profile.
• Protected routes (GET /profile) require Authorization header (JWT) or session cookie. Express middleware verifies token or session, then fetches user from MongoDB.
Architectural Patterns and Principles
• Layered architecture:
• Presentation (EJS views)
• Routing/controller layer
• Authentication middleware (Passport strategies)
• Data access layer (Mongoose models)
• Separation of concerns: config folder isolates Passport setups; models folder holds schema and auth helpers; views only handle UI.
• Stateless JWT for API protection; stateful sessions for OAuth flows.
Technologies and Frameworks to Annotate
• Node.js & Express.js
• Passport.js (local, Google, GitHub strategies)
• bcrypt (password hashing)
• JWT (jsonwebtoken)
• MongoDB + Mongoose
• express-session + connect-mongo
• EJS (view engine)
Essential Elements
• Component names (e.g., Passport Local Strategy, Mongoose User Model)
• Data flows labeled with HTTP methods and payload types (credentials, JWT)
• Authentication decision points (middleware that issues/verifies tokens)
• Distinction between synchronous DB calls and asynchronous OAuth interactions