This quickstart demonstrates how to authenticate users in a React Single-Page Application (SPA) using the Microsoft Authentication Library (msal-react) with Microsoft Entra ID, and how to access data securely via the Microsoft Graph API.
If you find this repository useful, please give it a star!
Category | Technology |
---|---|
Framework | Next.js 15.3.2 |
Language | TypeScript |
Authentication | MSAL React |
Identity | Microsoft Entra ID |
API | Microsoft Graph API |
UI Framework | Fluent UI |
Auth Flow | Authorization Code Flow with PKCE |
Data Retrieval | SWR |
- Node.js
- Next.js v15.3.2+
- Visual Studio Code
- A modern web browser
app/layout.tsx
– Root layout that wraps the appcomponents/AppClientShell.tsx
– The main client-side layout wrapper for the application. It sets up MSAL authentication, Fluent UI theming, and the core layout structure including the navigation bar and sidebar. It also handles client-side hydration with a loading overlay before rendering the app contentlib/msal/config.ts
– Defines the MSAL configuration used for authentication, includingclientId
,authority
, and scopes.
💡 If you're building your own application, this is where you'd replace theclientId
with your own App Registration ID.lib/msal/instance.ts
– Exports a singletonPublicClientApplication
instancelib/msal/helper.ts
– Helper functions for sign-in, sign-out, and token acquisitionlib/msgraph/endpoints.ts
- The Microsoft Graph API endpoints that may be calledlib/msgraph/fetcher.ts
- A typed fetcher function that performs authenticated GET requests to Microsoft Graph. It attaches a bearer token, handles response types (JSON, blob, image), and supports automatic pagination for OData @odata.nextLink responses.lib/msgraph/helper.ts
- Retrieves a Microsoft Graph access token using silent authentication with fallback to redirect if user interaction is required. Automatically sets the active account if one is not already set.
# Install dependencies from the root of the repo
npm install
# Run locally
npm run dev
# Optionally build
npm run build
- Open http://localhost:3000 to view in your browser.
- Open http://localhost:3000/profile/me to view a protected route, that if signed in will display profile information.