How wbmd-msoueilem/msalwithnextjs14 works
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).