How lalitdeveloper215-del/mini-authentication-system works
1. Overall shape
- This is a single Next.js 15 App Router application that contains both the UI and the mocked auth backend.
- There is no separate server service or database; route handlers under src/app/api/** act as the backend boundary.
2. User-facing surfaces
- Public pages: /, /login, /sso, and /unauthorized.
- Protected pages: /dashboard and /profile.
- The UI is composed from small client components in src/components and page-level route components in src/app/**.
3. Auth backend boundary
- Authentication logic is centralized in src/lib/auth/**.
- Key responsibilities split there are: mock user identity, token creation/validation, session helpers, and cookie management.
- This keeps the route handlers thin and makes the auth flow easy to diagram as “UI → API route → auth lib → cookies/session”.
4. Login and session establishment
- POST /api/auth/login validates the hardcoded mock credentials from src/lib/auth/mock-users.ts.
- On success it issues an access token and refresh token, then stores both in HTTP-only cookies.
- The login page (src/app/login/page.tsx plus LoginForm.tsx) is the entry point for this flow.
5. Protected request path
- Middleware in src/middleware.ts is the first protection layer for private routes.
- It checks whether the request has a valid access token, and if not, it can use the refresh token to mint a new access token automatically.
- If refresh fails, the user is redirected to /login or /unauthorized depending on the route.
6. Token and cookie model
- Tokens are not stored in browser JS state or localStorage; they live in HTTP-only cookies for safer handling.
- src/lib/auth/cookies.ts defines cookie behavior, while tokens.ts handles token generation/validation concerns.
- The README indicates a short-lived access token and a longer-lived refresh token, both scoped to the whole app.
7. Current-user and protected API endpoints
- GET /api/auth/me returns the authenticated user identity derived from the current token/cookie state.
- GET /api/protected represents a protected backend resource and is useful for showing authenticated API access separate from page navigation.
- These endpoints demonstrate that the auth layer protects both pages and API calls.
8. Logout and session teardown
- POST /api/auth/logout clears the auth cookies and ends the session.
- The LogoutButton component provides the UI action, while the server route performs the actual cookie invalidation.
- This is a clean example of client intent triggering server-side session removal.
9. Mock SSO handshake
- The SSO flow is intentionally conceptual, not tied to a real identity provider.
- GET /api/sso/start generates a state value and initiates the flow; GET /api/sso/callback completes it after validating the state and mock code.
- Successful SSO also results in auth tokens being issued, which makes SSO land in the same session model as password login.
10. Frontend auth state display
- AuthStatus, TokenDebugPanel, and LogoutButton are support components for showing session state and exercising the flow.
- The home page and auth pages are mostly orchestration and navigation around those components rather than business logic.
11. Testing and verification boundary
- tests/auth.spec.ts contains Playwright end-to-end coverage, which is important because the system’s core behavior spans browser, cookies, middleware, and route handlers.
- playwright.config.ts and the docs in docs/manual-testing.md / docs/project-flow.md show that the repo treats auth as an integrated flow, not isolated unit logic.
12. Practical architecture takeaway
- The main diagram should show four layers: browser/UI pages, Next.js middleware, API route handlers, and shared auth utilities.
- External-looking auth behavior is fully mocked inside the app, so the true system boundary is the Next.js process itself, with HTTP-only cookies carrying session state between requests.
Main entry points: session.ts Session · route.ts Login API · tokens.ts Tokens · auth.spec.ts Auth e2e
Components 25
UI routes
Next.js pages and client-side auth controls
- Home · Public landing page and orchestration shell
src/app/page.tsx - Login page · Public login surface
src/app/login/page.tsx - Login form · Submits credentials and starts password login
src/app/login/LoginForm.tsx - SSO page · Public SSO entry surface
src/app/sso/page.tsx - Dashboard · Authenticated app area
src/app/dashboard/page.tsx - Profile · Authenticated user profile area
src/app/profile/page.tsx - Unauthorized · Fallback when access is denied
src/app/unauthorized/page.tsx - Auth status · Displays current session and user state
src/components/AuthStatus.tsx - Token debug · Shows token/session diagnostics
src/components/TokenDebugPanel.tsx - Logout button · Triggers session teardown
src/components/LogoutButton.tsx
API routes
Route handlers for auth and protected backend access
- Login API · Validates credentials and issues tokens
src/app/api/auth/login/route.ts - Logout API · Clears auth cookies and ends the session
src/app/api/auth/logout/route.ts - Me API · Returns the current authenticated user
src/app/api/auth/me/route.ts - Refresh API · Mints a new access token from refresh state
src/app/api/auth/refresh/route.ts - Protected API · Authenticated backend resource
src/app/api/protected/route.ts - SSO start · Begins mock SSO with state generation
src/app/api/sso/start/route.ts - SSO callback · Completes mock SSO and issues tokens
src/app/api/sso/callback/route.ts
Auth core
Shared token, session, cookie, and mock identity logic
- Mock users · Hardcoded credentials and user identities
src/lib/auth/mock-users.ts - Tokens · Creates and validates access and refresh tokens
src/lib/auth/tokens.ts - Cookies · Defines HTTP-only auth cookie behavior
src/lib/auth/cookies.ts - Session · Builds and reads authenticated session state
src/lib/auth/session.ts - Auth types · Shared auth and session shapes
src/lib/auth/types.ts
Infra
Middleware, response helpers, and testing harness
- Middleware · Protects private routes and refreshes access tokens
src/middleware.ts - Response helper · Shared API response formatting
src/lib/api-response.ts - Auth e2e · End-to-end coverage for browser, cookies, middleware, and routes
tests/auth.spec.ts
Connections 33
- Home → Auth status: shows session · Home page presents auth state
- Home → Token debug: shows tokens · Home page exposes token diagnostics
- Home → Logout button: offers logout · Home page can trigger session teardown
- Login page → Login form: hosts form · Login page wraps the credential entry component
- Login form → Login API: POST credentials · Client submits login request
- SSO page → SSO start: start SSO · Entry page initiates the mock SSO flow
- Logout button → Logout API: POST logout · Client requests session removal
- Dashboard → Middleware: protected by · Private page access is mediated by middleware
- Profile → Middleware: protected by · Private page access is mediated by middleware
- Middleware → Tokens: validates token · Checks access token and refreshes if needed
- Middleware → Refresh API: refreshes via · Can mint a new access token when expired
- Login API → Mock users: checks creds · Login route validates the hardcoded user store
- Login API → Tokens: issues tokens · Successful login creates access and refresh tokens
- Login API → Cookies: sets cookies · Tokens are stored in HTTP-only cookies
- Login API → Session: builds session · Login establishes authenticated session state
- Logout API → Cookies: clears cookies · Logout invalidates auth cookies
- Me API → Session: reads session · Me endpoint derives identity from current auth state
- Protected API → Session: guards resource · Protected API requires authenticated session state
- Refresh API → Tokens: renews token · Refresh endpoint issues a new access token
- Refresh API → Cookies: updates cookies · Refresh response persists new auth cookies
- SSO start → Session: creates state · SSO start prepares temporary handshake state
- SSO callback → Session: verifies callback · Callback validates state and mock code
- SSO callback → Tokens: issues tokens · Successful SSO lands in the same session model
- SSO callback → Cookies: sets cookies · SSO completion writes auth cookies
- Session → Tokens: uses tokens · Session helper depends on token claims and expiry
- Session → Cookies: uses cookies · Session helper reads and writes cookie state
- Tokens → Auth types: shares shapes · Token logic uses shared auth types
- Session → Auth types: shares shapes · Session logic uses shared auth types
- Auth e2e → Middleware: verifies · E2E tests cover protection and refresh behavior
- Auth e2e → Login API: verifies · E2E tests cover login flow
- Auth e2e → SSO callback: verifies · E2E tests cover the SSO completion path
- Auth e2e → Logout API: verifies · E2E tests cover logout and cookie clearing
- Auth e2e → Me API: verifies · E2E tests cover authenticated identity lookup