# dhyaa-km/google-authentication architecture

> Architecture diagram of the GitHub repository dhyaa-km/google-authentication, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dhyaa-km/google-authentication
- Repository: https://github.com/dhyaa-km/google-authentication

## Overview

This is a full-stack Google OAuth authentication application. A user starts sign-in in the React frontend, is redirected through backend Passport routes to Google, and returns through a callback that validates and links or creates a MongoDB user before issuing an HTTP-only JWT cookie. The frontend then fetches the protected current-user endpoint and gates the dashboard. The graph also preserves logout and account-linking behavior. Some connections rely on README documentation or the SOURCE INDEX; unsampled implementation details are not inferred beyond those references.

Main entry points: [`frontend/src/App.tsx`](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/App.tsx) (App routes), [`frontend/src/context/AuthContext.tsx`](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/context/AuthContext.tsx) (Auth context), [`backend/src/routes/auth.route.ts`](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/routes/auth.route.ts) (Auth routes), [`backend/src/server.ts`](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/server.ts) (Express server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_app["App routes<br/>[App.tsx]"]
  node_login["Login page<br/>[Login.tsx]"]
  node_authcontext["Auth context<br/>[AuthContext.tsx]"]
  node_navbar["Navbar<br/>[Navbar.tsx]"]
  node_guard["Route guard<br/>[ProtectedRoute.tsx]"]
  node_dashboard["Dashboard<br/>[Dashboard.tsx]"]
  node_api["API client<br/>[api.ts]"]
end

subgraph group_auth["OAuth and JWT"]
  node_authroutes["Auth routes<br/>[auth.route.ts]"]
  node_passport["Google strategy<br/>[Passport.ts]"]
  node_callback["OAuth callback<br/>[authController.ts]"]
  node_userroutes["User routes<br/>[user.route.ts]"]
  node_middleware["JWT middleware<br/>[authmiddleware.ts]"]
end

subgraph group_accounts["Accounts and data"]
  node_authservice["Account linking<br/>[authService.ts]"]
  node_errors["Auth errors<br/>[authErrors.ts]"]
  node_userservice["User service<br/>[userService.ts]"]
  node_userdb[("User records<br/>[user.ts]")]
end

subgraph group_runtime["Backend runtime"]
  node_server["Express server<br/>[server.ts]"]
  node_config["Environment config<br/>[config.ts]"]
  node_dbconnect["Database connection<br/>[db.ts]"]
end

node_visitor(("User"))
node_google(("Google OAuth"))
node_mongodb[("MongoDB")]

node_visitor -->|"starts sign-in"| node_login
node_login -->|"uses auth state"| node_authcontext
node_visitor -->|"requests Google login"| node_authroutes
node_authroutes -->|"authenticates with"| node_passport
node_passport -->|"redirects to"| node_google
node_google -->|"returns callback"| node_authroutes
node_authroutes -->|"invokes callback"| node_callback
node_callback -->|"finds or links user"| node_authservice
node_authservice -->|"reads and writes"| node_userdb
node_authservice -->|"raises account errors"| node_errors
node_callback -->|"signs JWT and redirects"| node_config
node_callback -->|"sets cookie and redirects"| node_visitor
node_visitor -->|"opens dashboard"| node_app
node_app -->|"provides auth state"| node_authcontext
node_app -->|"renders"| node_navbar
node_app -->|"protects dashboard"| node_guard
node_app -->|"routes to"| node_login
node_app -->|"routes to"| node_dashboard
node_authcontext -->|"calls API"| node_api
node_api -->|"requests current user"| node_userroutes
node_authcontext -->|"posts logout"| node_api
node_api -->|"requests logout"| node_authroutes
node_guard -->|"checks auth state"| node_authcontext
node_dashboard -->|"uses auth state"| node_authcontext
node_server -->|"mounts routes"| node_authroutes
node_server -->|"mounts routes"| node_userroutes
node_server -->|"loads strategy"| node_passport
node_server -->|"connects database"| node_dbconnect
node_dbconnect -->|"reads connection settings"| node_config
node_dbconnect -->|"connects"| node_mongodb
node_userroutes -->|"requires JWT"| node_middleware
node_userroutes -->|"gets current user"| node_userservice
node_userservice -->|"loads user record"| node_userdb
node_middleware -->|"verifies JWT"| node_config

click node_app "https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/App.tsx"
click node_login "https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/pages/Login.tsx"
click node_authcontext "https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/context/AuthContext.tsx"
click node_navbar "https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/components/Navbar.tsx"
click node_guard "https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/components/ProtectedRoute.tsx"
click node_dashboard "https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/pages/Dashboard.tsx"
click node_api "https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/services/api.ts"
click node_authroutes "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/routes/auth.route.ts"
click node_passport "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/config/Passport.ts"
click node_callback "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/controllers/authController.ts"
click node_authservice "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/services/authService.ts"
click node_errors "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/errors/authErrors.ts"
click node_userservice "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/services/userService.ts"
click node_userdb "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/models/user.ts"
click node_userroutes "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/routes/user.route.ts"
click node_middleware "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/middleware/authmiddleware.ts"
click node_server "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/server.ts"
click node_config "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/config/config.ts"
click node_dbconnect "https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/config/db.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_login,node_authcontext,node_navbar,node_guard,node_dashboard,node_api,node_visitor toneBlue
class node_authroutes,node_passport,node_callback,node_userroutes,node_middleware,node_mongodb toneAmber
class node_authservice,node_errors,node_userservice,node_userdb toneMint
class node_server,node_config,node_dbconnect toneRose
class node_google toneIndigo
```

## Components

### Frontend

- [App routes](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Login page](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/pages/Login.tsx): `frontend/src/pages/Login.tsx`
- [Auth context](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/context/AuthContext.tsx): `frontend/src/context/AuthContext.tsx`
- [Navbar](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/components/Navbar.tsx): `frontend/src/components/Navbar.tsx`
- [Route guard](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/components/ProtectedRoute.tsx): `frontend/src/components/ProtectedRoute.tsx`
- [Dashboard](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/pages/Dashboard.tsx): `frontend/src/pages/Dashboard.tsx`
- [API client](https://github.com/dhyaa-km/google-authentication/blob/main/frontend/src/services/api.ts): `frontend/src/services/api.ts`

### OAuth and JWT

- [Auth routes](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/routes/auth.route.ts): `backend/src/routes/auth.route.ts`
- [Google strategy](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/config/Passport.ts): `backend/src/config/Passport.ts`
- [OAuth callback](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/controllers/authController.ts): `backend/src/controllers/authController.ts`
- [User routes](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/routes/user.route.ts): `backend/src/routes/user.route.ts`
- [JWT middleware](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/middleware/authmiddleware.ts): `backend/src/middleware/authmiddleware.ts`

### Accounts and data

- [Account linking](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/services/authService.ts): `backend/src/services/authService.ts`
- [Auth errors](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/errors/authErrors.ts): `backend/src/errors/authErrors.ts`
- [User service](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/services/userService.ts): `backend/src/services/userService.ts`
- [User records](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/models/user.ts): `backend/src/models/user.ts`

### Backend runtime

- [Express server](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/server.ts): `backend/src/server.ts`
- [Environment config](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/config/config.ts): `backend/src/config/config.ts`
- [Database connection](https://github.com/dhyaa-km/google-authentication/blob/main/backend/src/config/db.ts): `backend/src/config/db.ts`

### Other components

- **User**
- **Google OAuth**
- **MongoDB**

## Connections

- User → Login page: starts sign-in (evidence: `readme.md`)
- Login page → Auth context: uses auth state (evidence: `frontend/src/pages/Login.tsx`)
- User → Auth routes: requests Google login (evidence: `readme.md`)
- Auth routes → Google strategy: authenticates with (evidence: `backend/src/routes/auth.route.ts`)
- Google strategy → Google OAuth: redirects to (evidence: `readme.md`)
- Google OAuth → Auth routes: returns callback (evidence: `readme.md`)
- Auth routes → OAuth callback: invokes callback (evidence: `backend/src/routes/auth.route.ts`)
- OAuth callback → Account linking: finds or links user (evidence: `backend/src/controllers/authController.ts`)
- Account linking → User records: reads and writes (evidence: `backend/src/services/authService.ts`)
- Account linking → Auth errors: raises account errors (evidence: `backend/src/services/authService.ts`)
- OAuth callback → Environment config: signs JWT and redirects (evidence: `backend/src/controllers/authController.ts`)
- OAuth callback → User: sets cookie and redirects (evidence: `readme.md`)
- User → App routes: opens dashboard (evidence: `readme.md`)
- App routes → Auth context: provides auth state (evidence: `frontend/src/App.tsx`)
- App routes → Navbar: renders (evidence: `frontend/src/App.tsx`)
- App routes → Route guard: protects dashboard (evidence: `frontend/src/App.tsx`)
- App routes → Login page: routes to (evidence: `frontend/src/App.tsx`)
- App routes → Dashboard: routes to (evidence: `frontend/src/App.tsx`)
- Auth context → API client: calls API (evidence: `frontend/src/context/AuthContext.tsx`)
- API client → User routes: requests current user (evidence: `readme.md`)
- Auth context → API client: posts logout (evidence: `frontend/src/context/AuthContext.tsx`)
- API client → Auth routes: requests logout (evidence: `readme.md`)
- Route guard → Auth context: checks auth state (evidence: `frontend/src/components/ProtectedRoute.tsx`)
- Dashboard → Auth context: uses auth state (evidence: `frontend/src/pages/Dashboard.tsx`)
- Express server → Auth routes: mounts routes (evidence: `backend/src/server.ts`)
- Express server → User routes: mounts routes (evidence: `backend/src/server.ts`)
- Express server → Google strategy: loads strategy (evidence: `backend/src/server.ts`)
- Express server → Database connection: connects database (evidence: `backend/src/server.ts`)
- Database connection → Environment config: reads connection settings (evidence: `backend/src/config/db.ts`)
- Database connection → MongoDB: connects (evidence: `backend/src/config/db.ts`)
- User routes → JWT middleware: requires JWT (evidence: `backend/src/routes/user.route.ts`)
- User routes → User service: gets current user (evidence: `backend/src/routes/user.route.ts`)
- User service → User records: loads user record (evidence: `backend/src/services/userService.ts`)
- JWT middleware → Environment config: verifies JWT (evidence: `backend/src/middleware/authmiddleware.ts`)
