# wbmd-msoueilem/msalwithnextjs14 architecture

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

- Interactive diagram: https://gitdiagram.com/wbmd-msoueilem/msalwithnextjs14
- Repository: https://github.com/wbmd-msoueilem/msalwithnextjs14

## Overview

##### Project Type and Purpose
- This is a server-rendered, full-stack React application using Next.js v14’s App Router.
- Its primary purpose is to provide Azure AD authentication via MSAL (Microsoft Authentication Library), plus demos of token retrieval and role-based access control.

##### File Structure
- app/ – Next.js App Router directory containing page.tsx files (UI entry points).
- components/ – Reusable React components (buttons, avatar, role container, unauthorized message).
- msal/ – All MSAL integration logic, including authentication configuration, provider, token fetching, Graph queries, and user-helper functions.
- public/ – Static assets.
- globals.css, tailwind.config.ts, postcss.config.js – Styling via Tailwind CSS.
- next.config.js, tsconfig.json, .env – Next.js configuration and environment variables for Azure IDs and roles.

##### README for Stack & Flow
- Next.js v14, React v18, @azure/msal-browser & msal-react for authentication.
- App is bootstrapped by create-next-app.
- MSAL config is read from msal/authConfig.js and environment variables for tenant/client IDs and admin role name.

##### Main Components & Data Flows
a. Frontend (Next.js App)
– app/layout.tsx wraps all pages with the MSAL React provider (MyMsalProvider).
– A set of pages:
- Public pages (app/page.tsx)
- Role-protected pages (app/adminonlypage, app/gettokentest, app/adventureworks)
– Reusable components for sign-in/out, user avatar, and role-based rendering.
b. MSAL React Provider (MyMsalProvider.tsx)
– Initializes PublicClientApplication with authConfig.js.
– Exposes authentication context to all child components.
c. Azure AD / MSAL Endpoints
– @azure/msal-browser communicates with Azure endpoints for login, logout, token acquisition.
d. Token Fetcher & Graph Query
– tokenFetcher.ts requests tokens silently or via popup.
– msalGraph.js issues Graph API calls for testing token scopes.
e. Environment Variables
– NEXT_PUBLIC_USER_CLAIMS_ADMIN defines the role name used in UserRolesContainer.

##### Architectural Patterns & Design Principles
- App Router (file-system routing) for SSR/SSG and client routing.
- Provider Pattern: React Context via MSAL React.
- Separation of Concerns:
– UI pages vs. reusable components vs. authentication logic.
– Styling isolated in globals.css and Tailwind config.
- Role-based access implemented via a dedicated component (UserRolesContainer).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Browser["Client Browser"]:::external

    %% Next.js Server
    subgraph "Next.js Server Runtime" 
        direction TB
        Layout["Layout\n(app/layout.tsx)"]:::frontend
        Provider["MSAL React Provider\n(msal/MyMsalProvider.tsx)"]:::auth
        subgraph "Pages"
            direction TB
            Home["Public Home Page\n(app/page.tsx)"]:::frontend
            AdminPage["Admin-Only Page\n(app/adminonlypage/page.tsx)"]:::frontend
            TokenTest["Get Token Test Page\n(app/gettokentest/page.tsx)"]:::frontend
            Adventure["AdventureWorks Demo Page\n(app/adventureworks/page.tsx)"]:::frontend
        end
        Layout --> Provider
        Provider --> Home
        Provider --> AdminPage
        Provider --> TokenTest
        Provider --> Adventure
    end

    %% UI Components
    subgraph "UI Components"
        direction TB
        SignIn["SignInButton\n(components/SignInButton.jsx)"]:::frontend
        SignInOut["SignInSignOutButton\n(components/SignInSignOutButton.jsx)"]:::frontend
        SignOut["SignOutButton\n(components/SignOutButton.tsx)"]:::frontend
        Avatar["UserAvatar\n(components/UserAvatar.tsx)"]:::frontend
        RolesContainer["UserRolesContainer\n(components/UserRolesContainer.tsx)"]:::frontend
        Unauthorized["UnauthorizedMessage\n(components/UnauthorizedMessage.tsx)"]:::frontend
    end

    %% Auth Logic
    subgraph "Authentication Logic"
        direction TB
        AuthConfig["authConfig\n(msal/authConfig.js)"]:::config
        MSALCore["MSAL Core Init\n(msal/msal.ts)"]:::auth
        TokenFetcher["Token Fetcher\n(msal/tokenFetcher.ts)"]:::auth
        GraphHelper["Graph API Helper\n(msal/msalGraph.js)"]:::auth
        QueryUtils["Query Utils\n(msal/query.ts)"]:::auth
        UserHelper["User Helper\n(msal/userHelper.ts)"]:::auth
    end

    %% Static Assets & Config
    subgraph "Configuration & Assets"
        direction TB
        Env["Environment Variables\n(.env)"]:::config
        NextConfig["next.config.js"]:::config
        TSConfig["tsconfig.json"]:::config
        Tailwind["tailwind.config.ts"]:::config
        PostCSS["postcss.config.js"]:::config
        Globals["globals.css"]:::config
        Public["Public Assets\n(public/)"]:::external
    end

    %% External Services
    AzureAD["Azure AD\n(login.microsoftonline.com)"]:::external
    GraphAPI["Microsoft Graph\n(graph.microsoft.com)"]:::external

    %% Data Flows
    Browser -->|"SSR Request"| Layout
    Layout -->|"Hydration"| Browser
    Provider -->|"Init Pca with config"| MSALCore
    MSALCore --> AuthConfig
    AuthConfig --> Env
    TokenFetcher --> MSALCore
    TokenFetcher --> AuthConfig
    GraphHelper --> TokenFetcher
    GraphHelper --> QueryUtils
    GraphHelper -->|"Call"| GraphAPI
    Provider -->|"Acquire Token"| AzureAD

    %% Page Guards and Components Usage
    AdminPage -->|"guard"| RolesContainer
    TokenTest -->|"uses"| TokenFetcher
    Adventure -->|"uses Graph"| GraphHelper
    Home --> SignInOut
    Home --> SignIn
    AdminPage --> SignOut
    AdminPage --> Avatar
    AdminPage --> Unauthorized

    %% Click Events
    click Layout "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/app/layout.tsx"
    click Home "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/app/page.tsx"
    click AdminPage "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/app/adminonlypage/page.tsx"
    click TokenTest "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/app/gettokentest/page.tsx"
    click Adventure "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/app/adventureworks/page.tsx"
    click Globals "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/app/globals.css"
    click Provider "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/msal/MyMsalProvider.tsx"
    click AuthConfig "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/msal/authConfig.js"
    click MSALCore "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/msal/msal.ts"
    click TokenFetcher "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/msal/tokenFetcher.ts"
    click GraphHelper "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/msal/msalGraph.js"
    click QueryUtils "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/msal/query.ts"
    click UserHelper "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/msal/userHelper.ts"
    click SignIn "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/components/SignInButton.jsx"
    click SignInOut "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/components/SignInSignOutButton.jsx"
    click SignOut "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/components/SignOutButton.tsx"
    click Unauthorized "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/components/UnauthorizedMessage.tsx"
    click Avatar "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/components/UserAvatar.tsx"
    click RolesContainer "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/components/UserRolesContainer.tsx"
    click Tailwind "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/tailwind.config.ts"
    click PostCSS "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/postcss.config.js"
    click NextConfig "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/next.config.js"
    click TSConfig "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/tsconfig.json"
    click Env "https://github.com/wbmd-msoueilem/msalwithnextjs14/blob/master/.env"

    %% Styles
    classDef frontend fill:#D0E6FF,stroke:#0366D6,color:#0366D6;
    classDef auth fill:#DFFFE0,stroke:#219653,color:#219653;
    classDef external fill:#FFEBD0,stroke:#F2994A,color:#F2994A;
    classDef config fill:#E0E0E0,stroke:#666666,color:#333333;
```
