# techinho/multi-auth-passportjs architecture

> Architecture diagram of the GitHub repository techinho/multi-auth-passportjs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/techinho/multi-auth-passportjs
- Repository: https://github.com/techinho/multi-auth-passportjs

## Overview

##### 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

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Side
    subgraph "Client (Browser)" 
        direction TB
        Browser["Browser"]:::client
        Home["home.ejs"]:::client
        Login["login.ejs"]:::client
        Register["register.ejs"]:::client
        ProfileView["profile.ejs"]:::client
        LocalStorage["LocalStorage (JWT)"]:::client
    end

    %% Server Side
    subgraph "Web Server (Node.js/Express)"
        direction TB
        App["app.js"]:::server
        Config[".env / Config Vars"]:::server
        PassportConfig["passport-setup.js"]:::server
        subgraph "Routes"
            direction TB
            AuthRoutes["auth-routes.js"]:::server
            ProfileRoutes["profile-routes.js"]:::server
        end
        Models["user-model.js"]:::server
        SessionStore["Session Store (connect-mongo)"]:::server
        Middleware["JWT/Session Middleware"]:::server
    end

    %% Database
    subgraph "Database"
        direction TB
        MongoDB["MongoDB"]:::db
    end

    %% External OAuth Providers
    subgraph "External Services"
        direction TB
        Google["Google OAuth 2.0"]:::external
        GitHub["GitHub OAuth"]:::external
    end

    %% Data Flows
    Browser -->|"GET /, /login, /register"| App
    App -->|"Renders EJS"| Home
    App -->|"Renders EJS"| Login
    App -->|"Renders EJS"| Register
    App -->|"Renders EJS"| ProfileView
    Browser -->|"Stores token"| LocalStorage

    Browser -->|"POST /auth/login (credentials)"| AuthRoutes
    AuthRoutes -->|"Local Strategy"| PassportConfig
    PassportConfig -->|"bcrypt verify"| Models
    AuthRoutes -->|"Issue JWT"| Middleware
    Middleware -->|"Return JWT"| Browser

    Browser -->|"GET /auth/google"| AuthRoutes
    AuthRoutes -->|"Redirect"| Google
    Google -->|"Code exchange"| AuthRoutes
    AuthRoutes -->|"Passport Google Strategy"| PassportConfig
    AuthRoutes -->|"Upsert user"| Models
    AuthRoutes -->|"Issue JWT"| Middleware
    AuthRoutes -->|"Redirect to profile"| Browser

    Browser -->|"GET /profile (Auth header)"| ProfileRoutes
    ProfileRoutes -->|"Verify JWT/Session"| Middleware
    Middleware -->|"Fetch user"| Models
    ProfileRoutes -->|"Respond profile data"| Browser

    Models -->|"Read/Write"| MongoDB
    App -->|"Session config"| SessionStore
    SessionStore -->|"Persist sessions"| MongoDB
    AuthRoutes -->|"Uses env vars"| Config
    PassportConfig -->|"Uses env vars"| Config

    %% Click Events
    click Home "https://github.com/techinho/multi-auth-passportjs/blob/main/views/home.ejs"
    click Login "https://github.com/techinho/multi-auth-passportjs/blob/main/views/login.ejs"
    click Register "https://github.com/techinho/multi-auth-passportjs/blob/main/views/register.ejs"
    click ProfileView "https://github.com/techinho/multi-auth-passportjs/blob/main/views/profile.ejs"
    click App "https://github.com/techinho/multi-auth-passportjs/blob/main/app.js"
    click AuthRoutes "https://github.com/techinho/multi-auth-passportjs/blob/main/routes/auth-routes.js"
    click ProfileRoutes "https://github.com/techinho/multi-auth-passportjs/blob/main/routes/profile-routes.js"
    click PassportConfig "https://github.com/techinho/multi-auth-passportjs/blob/main/config/passport-setup.js"
    click Models "https://github.com/techinho/multi-auth-passportjs/blob/main/models/user-model.js"

    %% Styles
    classDef client fill:#ccf,stroke:#333,stroke-width:1px
    classDef server fill:#cfc,stroke:#333,stroke-width:1px
    classDef db fill:#eee,stroke:#333,stroke-width:1px
    classDef external fill:#fc9,stroke:#333,stroke-width:1px
```
