Giter Site home page Giter Site logo

dwarfered / ms-identity-nextjs-sample Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 444 KB

An example showcasing the core principles of modern authentication using the Microsoft Identity Platform in Next.js version 14, utilizing MSAL React for client-side requests to Microsoft Graph.

Home Page: https://www.linkedin.com/in/chris-dymond

License: MIT License

msal-react nextjs14 msal-authentication ms-graph-api msal pkce-flow

ms-identity-nextjs-sample's Introduction

Enable your Next.js Single-Page Application (SPA) to sign-in users and call APIs with the Microsoft identity platform

An example showcasing the core principles of modern authentication using the Microsoft Identity Platform in Next.js version 14, utilizing MSAL React for client-side requests to Microsoft Graph.

The Microsoft identity platform, incorporating Entra ID (Azure AD) and Azure Azure Active Directory B2C (Azure AD B2C) plays a pivotal role in the Azure cloud ecosystem. This example is designed to showcase the basics of modern authentication within a Next.js framework, utilising the Microsoft Authentication Library for React (MSAL React).

Next.js is an open-source React front-end development framework that enables developers to build server-side rendering and static web applications. It's known for its features like automatic code splitting, optimized performance, and simple page-based routing system, as well as for supporting static site generation (SSG), server-side rendering (SSR), and incremental static regeneration (ISR), making it a versatile choice for developing highly efficient and scalable web applications.

Sample Prerequisites

Recommendations

ℹī¸ "SWR is a strategy to first return the data from cache (stale), then send the fetch request (revalidate), and finally come with the up-to-date data."

Notable files

  1. ./authConfig.ts Configuration of MSAL
  2. ./utils/NavigationClient.ts The AppRouterInstance and Next.js
  3. ./utils/msGraphFetcher.ts Implementation of the SWR Fetcher instance for MS Graph calls including the access token.
  4. ./utils/MsalHelper.ts Token acquisition from the MSAL library.
  5. ./components/ProfileData.ts Consuming the SWR service without needing to use props from a parent component.

Configure the application

  • Open ./authConfig.ts in an editor.
  • Replace NEXTJS_APP_CLIENT_ID with the Application (client) ID from your created Entra Application Registration.
  • Replace NEXTJS_APP_AUTHORITY with the Entra Tenant Id of your environment.

Installing dependencies

# Install dependencies from the root of the repo
npm install

Running the application

# Run locally
npm run dev

# Optionally build
# npm run build
  1. Open http://localhost:3000 to view in your browser.
  2. Open http://localhost:3000/profile to view a protected route, that if signed in will display profile information.
  3. Open http://localhost:3000/example to see an unprotected route.
  • The current version of Next.js does not correctly handle redirect requests. So in this sample the popup method is used.

Screenshots

Pending Sign In

Sign In

Signed In

Request Profile Information

ms-identity-nextjs-sample's People

Contributors

dwarfered avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ms-identity-nextjs-sample's Issues

How to make application permission requests

Is there a way to make server side application permission Graph requests (e.g. get Entra id users, create an item on a SharePoint list, etc.)? These would be actions that the signed in user may not have permissions for, which is why it would be done under application Graph api permissions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.