Skip to content

devopsbob/msal-nextjs15-sample

Repository files navigation

MSAL.js for Next.js 15.x Sample - Authorization Code Flow in Single-Page Applications

About this sample

This developer sample is used to run basic use cases for the MSAL library. You can also alter the configuration in ./src/authConfig.js to execute other behaviors. This sample was bootstrapped with microsoft-authentication-library-for-js.

Notable files and what they demonstrate

  1. ./src/app/layout.tsx - Shows implementation of MsalProvider, all children will have access to @azure/msal-react context, hooks and components
  2. ./src/app/home-page.tsx - Homepage, shows how to conditionally render content using AuthenticatedTemplate and UnauthenticatedTemplate depending on whether or not a user is signed in.
  3. ./src/app/profile/page.tsx - Example of a protected route using MsalAuthenticationTemplate. If a user is not yet signed in, signin will be invoked automatically. If a user is signed in it will acquire an access token and make a call to MS Graph to fetch user profile data.
  4. ./src/authConfig.js - Configuration options for PublicClientApplication and token requests
  5. ./src/ui-components/SignInSignOutButton.jsx - Example of how to conditionally render a Sign In or Sign Out button using the useIsAuthenticated hook.
  6. ./src/ui-components/SignInButton.jsx - Example of how to get the PublicClientApplication instance using the useMsal hook and invoking a login function.
  7. ./src/ui-components/SignOutButton.jsx - Example of how to get the PublicClientApplication instance using the useMsal hook and invoking a logout function.
  8. ./src/utils/MsGraphApiCall.js - Example of how to call the MS Graph API with an access token.
  9. ./src/utils/NavigationClient.js - Example implementation of INavigationClient which can be used to override the default navigation functions MSAL.js uses

How to run the sample

Pre-requisites

Configure the application

  • Open ./src/authConfig.js in an editor.
  • Replace client id with the Application (client) ID from the portal registration, or use the currently configured lab registration.
    • Optionally, you may replace any of the other parameters, or you can remove them and use the default values.

Install npm dependencies for sample

# Install dev dependencies for msal-react and msal-browser from root of repo
npm install

# Change directory to sample directory
cd samples/msal-react-samples/nextjs15-sample

# Build packages locally
npm run build:package

Running the sample development server

  1. In a command prompt, run npm run dev.
  2. Open http://localhost:3000 to view it in the browser.
  3. Open http://localhost:3000/profile to see an example of a protected route. If you are not yet signed in, signin will be invoked automatically.

The page will reload if you make edits. You will also see any lint errors in the console.

  • In the web page, click on the "Login" button and select either Sign in using Popup or Sign in using Redirect to begin the auth flow.

Running the sample production server

  1. In a command prompt, run npm run build.
  2. Next run npm start
  3. Open http://localhost:3000 to view it in the browser.
  4. Open http://localhost:3000/profile to see an example of a protected route. If you are not yet signed in, signin will be invoked automatically.

Learn More about Next.js

You can learn more in the Next.js documentation.

To learn React, check out the React documentation.

About

MSAL NextJS15 sample using Node 22 and NPM 11

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published