# rupeshbisen/next-js-authentication architecture

> Architecture diagram of the GitHub repository rupeshbisen/next-js-authentication, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rupeshbisen/next-js-authentication
- Repository: https://github.com/rupeshbisen/next-js-authentication

## Overview

##### Project Type and Purpose
- This is a full-stack Next.js application.
- It provides user registration, login, and forgot-password flows via both React UI pages (under src/app) and serverless API routes (under src/app/api).
- It uses a database (models and src/database) to persist users and OTPs, plus middleware for authentication.

##### File Structure
Top-level:
- next.config.js, tsconfig.json, Tailwind config → build/system config
- public/ → static assets
- src/app/ → frontend pages & API routes, using Next.js App Router
- src/components/ → shared React components (Navbar, Input, Loader, Notification)
- src/context/ → React Context for global state (e.g. auth)
- src/database/ → DB connection setup
- src/middleware/ → Next.js edge middleware for AuthUser
- src/models/ → data schemas (user.model, otp.model)
- src/service/ → service-layer logic for register, login, forgot-password
- src/types/ → TypeScript interfaces for request/response payloads
- src/utils/ → helper functions

##### README for Additional Insights
- Bootstrapped with create-next-app.
- Uses next/font, Tailwind CSS.
- Standard dev and deployment instructions (npm run dev, vercel).

##### Detail and Thoroughness
- Every distinct part of the stack (pages, layout, components, services, models, middleware, utils).
- How context providers (src/context) feed into the UI for auth state.
- Configuration files (next.config.js, tailwind.config.ts) only if the deployment/configuration layer is in scope.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend UI Layer
    subgraph "Frontend UI" 
        direction TB
        Layout["Global Layout"]:::ui
        LandingPage["Landing Page"]:::ui
        RegisterPage["Register Page"]:::ui
        LoginPage["Login Page"]:::ui
        ForgotPage["Forgot-Password Page"]:::ui
        Navbar["Navbar"]:::ui
        Notification["Notification"]:::ui
        InputComponent["Input Component"]:::ui
        Loader["Loader"]:::ui
        AuthContext["Auth Context Provider"]:::ui
    end

    %% API Layer
    subgraph "Next.js Serverless API" 
        direction TB
        API_Register["/api/register Route"]:::api
        API_Login["/api/login Route"]:::api
        API_SendOTP["/api/forgot-password/sendotp Route"]:::api
        API_SetPassword["/api/forgot-password/setpassword Route"]:::api
    end

    %% Middleware Layer
    AuthUser["AuthUser Middleware"]:::middleware

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        RegisterService["Register Service"]:::service
        LoginService["Login Service"]:::service
        ForgotService["Forgot-Password Service"]:::service
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        DBConnection["Database Connection"]:::data
        UserModel["User Model"]:::data
        OTPModel["OTP Model"]:::data
    end

    %% External Services
    OTPProvider["External OTP/Email Provider"]:::external

    %% Connections: UI to API
    LandingPage -->|navigates| Layout
    Layout -->|renders| Navbar
    Layout -->|renders| Notification
    Layout -->|renders| InputComponent
    Layout -->|renders| Loader
    Layout -->|uses| AuthContext

    RegisterPage -->|calls| API_Register
    LoginPage -->|calls| API_Login
    ForgotPage -->|calls| API_SendOTP
    ForgotPage -->|calls| API_SetPassword

    %% API to Middleware
    API_Register -->|protected by| AuthUser
    API_Login -->|protected by| AuthUser
    API_SendOTP -->|protected by| AuthUser
    API_SetPassword -->|protected by| AuthUser

    %% API to Services
    API_Register -->|uses| RegisterService
    API_Login -->|uses| LoginService
    API_SendOTP -->|uses| ForgotService
    API_SetPassword -->|uses| ForgotService

    %% Services to Data Layer
    RegisterService -->|reads/writes| UserModel
    LoginService -->|reads| UserModel
    ForgotService -->|reads/writes| OTPModel
    ForgotService -->|uses| DBConnection
    RegisterService -->|uses| DBConnection
    LoginService -->|uses| DBConnection

    %% Service to External
    ForgotService -->|sends OTP| OTPProvider
    OTPProvider -->|returns status| ForgotService

    %% Styles
    classDef ui fill:#E0F7FA,stroke:#0288D1,color:#01579B
    classDef api fill:#E8F5E9,stroke:#43A047,color:#1B5E20
    classDef service fill:#FFF3E0,stroke:#FB8C00,color:#E65100
    classDef data fill:#EEEEEE,stroke:#757575,color:#424242
    classDef external fill:#F3E5F5,stroke:#8E24AA,color:#4A148C
    classDef middleware fill:#ECEFF1,stroke:#546E7A,color:#263238

    %% Click Events: Frontend Pages & Layout
    click Layout "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/layout.tsx"
    click LandingPage "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/page.tsx"
    click RegisterPage "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/register/page.tsx"
    click LoginPage "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/login/page.tsx"
    click ForgotPage "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/forgot-password/page.tsx"
    click Navbar "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/components/Navbar.tsx"
    click Notification "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/components/Notification.tsx"
    click InputComponent "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/components/formElements/InputComponent.tsx"
    click Loader "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/components/loader/ComponentLevelLoader.tsx"
    click AuthContext "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/context/index.tsx"

    %% Click Events: API Routes
    click API_Register "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/api/register/route.tsx"
    click API_Login "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/api/login/route.tsx"
    click API_SendOTP "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/api/forgot-password/sendotp/route.tsx"
    click API_SetPassword "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/app/api/forgot-password/setpassword/route.tsx"

    %% Click Events: Service Layer
    click RegisterService "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/service/register/index.tsx"
    click LoginService "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/service/login/index.tsx"
    click ForgotService "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/service/forgot-password/index.tsx"

    %% Click Events: Data Layer
    click DBConnection "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/database/index.tsx"
    click UserModel "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/models/user.model.tsx"
    click OTPModel "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/models/otp.model.tsx"

    %% Click Events: Middleware
    click AuthUser "https://github.com/rupeshbisen/next-js-authentication/blob/main/src/middleware/AuthUser.tsx"

    %% Note: Types & Utils not shown as primary nodes but part of implementation
```
