How pankajgurav005/full-stack-nestjs-nextjs works
1. This repository is a two-tier fullstack app: a NestJS backend in backend/ and a Next.js frontend in frontend/. The main architectural boundary is the HTTP API between them; the frontend consumes backend endpoints for authentication and user/profile flows.
2. The backend is modular NestJS. app.module.ts wires together feature modules such as auth, users, roles, hello, plus infrastructure like database.module.ts. This suggests a standard controller/service/module layering with shared cross-cutting concerns in src/common and src/utils.
3. Data persistence is MongoDB via Mongoose. The backend contains schemas for users, refreshtoken, and roles, so the core domain model is centered on user accounts, role assignment, and token storage/rotation. config/database.module.ts is the main database boundary.
4. Authentication is a major subsystem. The auth module contains controller, service, DTOs, and JWT-related constants, while common/guards/at.guard.ts and rt.guard.ts indicate access-token and refresh-token protection. The presence of get-current-user* decorators shows authenticated requests are expected to inject identity context into handlers.
5. User management is separated from auth. The users module owns user-related controllers/services, creation DTOs, and the user and refresh-token schemas. In practice, auth handles sign-in/sign-up and token issuance, while users handles persistent user profile operations and may support role-based access control.
6. Roles are modeled as a first-class module with its own schema and controller. That implies authorization is not just JWT-based but also role-aware, with role data stored server-side and likely consulted by guards or service logic before serving protected resources.
7. Cross-cutting backend infrastructure includes logging, mail, exception handling, and middleware. common/service/logger.service.ts, loggermiddleware.service.ts, and mail.service.ts show operational concerns around request logging and outbound email. utils/http-exception.filter.ts centralizes error shaping, which matters for API consistency across modules.
8. There is a seeding path for initial data. src/seeder/seeder.ts and users.seeder.ts indicate bootstrap scripts that populate the database, likely for test/demo environments or first-run setup. This is relevant to the architecture because it sits outside the request path but influences initial system state.
9. The backend also includes sample and test-oriented endpoints: hello appears to be a lightweight health/demo module, while backend/test and the *.spec.ts files show unit and e2e coverage with Jest. The repo also includes Docker-related files and multiple .env.* variants, so deployment/configuration is environment-driven.
10. The frontend uses Next.js App Router. Pages are organized by route segments such as signin, signup, noaccess, and a protected area under app/(protected). The protected layout wraps authenticated views like profile and about, which means route grouping is used to separate public and authenticated UI shells.
11. Frontend state and API integration are centralized. redux/store.js, slice.js, and providers.js indicate a Redux-based client state layer, while utils/api.js is the likely HTTP client boundary to the NestJS backend. common/Modalmanager.js and the shared components in app/components suggest UI state like dialogs is managed outside individual pages.
12. The UI composition is split into reusable layout components and page-level views. Header, Sidebar, and Modal form shared presentation scaffolding, while global styles are handled through globals.css, styles/global.css, and Tailwind config. That makes the frontend architecture more shell-and-pages than a deeply nested component tree.
13. End-to-end, the main user flow is: public signup/login page on the frontend → API call to backend auth endpoints → backend validates credentials, persists or reads user data in MongoDB, issues tokens, and applies role/user guards for protected routes → frontend renders protected pages based on auth state. The noaccess page suggests authorization failures are handled explicitly in the UI.
14. For an architecture diagram, the key boxes are: Next.js public/protected UI, Redux/API client, NestJS API layer, auth/users/roles modules, MongoDB schemas, and auxiliary services for logging, mail, seeding, and exception handling. The strongest boundaries are frontend vs backend, auth vs domain modules, and request handling vs infrastructure concerns.
Main entry points: api.js API client · app.module.ts App module · auth.module.ts Auth module · users.module.ts Users module
Components 23
Frontend
Next.js App Router UI and client state
- App Router · Public and protected route structure
frontend/app - Public pages · Signin, signup, and landing entry points
frontend/app/page.js - Protected area · Authenticated app shell for guarded views
frontend/app/(protected) - Profile · Authenticated user profile view
frontend/app/(protected)/profile/page.js - About · Protected informational page
frontend/app/(protected)/about/page.js - Auth pages · Login and registration screens
frontend/app/signin/page.js - No access · UI for authorization failures
frontend/app/noaccess/page.js - Shared shell · Header, sidebar, modal, and protected layout
frontend/app/components - Client state · Redux store, slice, and providers
frontend/redux/store.js - API client · Frontend boundary to backend endpoints
frontend/utils/api.js
Backend
NestJS API, domain modules, and infrastructure
- Nest bootstrap · Application startup and global wiring
backend/src/main.ts - App module · Top-level Nest module assembly
backend/src/app.module.ts - Auth module · Sign-in, token issuance, and auth controllers/services
backend/src/auth/auth.module.ts - Users module · User profile and account persistence logic
backend/src/users/users.module.ts - Roles module · Role data and role-aware access control
backend/src/roles/roles.module.ts - Database module · Persistence wiring and connection setup
backend/src/config/database.module.ts - Common layer · Shared decorators, guards, logging, and mail
backend/src/common - Exception filter · Centralized HTTP error shaping
backend/src/utils/http-exception.filter.ts - Seeder · Initial database population scripts
backend/src/seeder/seeder.ts - Hello module · Lightweight sample or health-style module
backend/src/hello/hello.module.ts - User schema · Persistent user account model
backend/src/users/schemas/user.schema.ts - Refresh token · Stored refresh token model
backend/src/users/schemas/refreshtoken.schema.ts - Role schema · Stored role model
backend/src/roles/schemas/roles.schema.ts
Connections 30
- Public pages → API client: auth requests · Public screens submit sign-in and sign-up calls
- Auth pages → API client: credentials · Authentication forms call backend auth endpoints
- Profile → API client: profile fetch · Protected views load user data from the API
- Client state → API client: dispatches · Redux state is fed by API responses
- Protected area → Shared shell: wraps · Protected route group uses shared app shell
- App Router → Public pages: contains · App router contains public entry routes
- App Router → Protected area: contains · App router contains authenticated routes
- Protected area → Profile: serves · Protected area includes profile page
- Protected area → About: serves · Protected area includes about page
- No access → API client: auth state · No-access page reflects authorization outcome
- Nest bootstrap → App module: boots · Entry point starts the Nest application
- App module → Database module: connects · App module wires database infrastructure
- App module → Auth module: imports · Top-level composition includes auth module
- App module → Users module: imports · Top-level composition includes users module
- App module → Roles module: imports · Top-level composition includes roles module
- App module → Hello module: imports · Top-level composition includes hello module
- Auth module → User schema: reads/writes · Auth uses user records for sign-in and signup
- Auth module → Refresh token: issues/rotates · Auth manages refresh token lifecycle
- Users module → User schema: owns · Users module owns persistent user model
- Users module → Refresh token: stores · Users module manages refresh token storage
- Roles module → Role schema: owns · Roles module owns role model
- Auth module → Common layer: uses · Auth depends on guards and decorators
- Users module → Common layer: uses · Users depend on shared request context helpers
- Roles module → Common layer: uses · Roles may rely on guards and shared middleware
- App module → Exception filter: global filter · Application applies centralized error handling
- Seeder → User schema: seeds · Bootstrap data populates users
- Seeder → Role schema: seeds · Bootstrap data populates roles
- API client → Auth module: HTTP API · Frontend consumes backend auth endpoints
- API client → Users module: HTTP API · Frontend consumes user/profile endpoints
- API client → Roles module: HTTP API · Frontend may consume role-aware endpoints