Skip to content

nikolaidk/react_msal_b2c_demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MSAL.js for React B2C Sample - Authorization Code Flow in Single-Page Applications

About this sample

This developer sample is used to run basic B2C 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 Create React App.

How to handle B2C user-flows

Implementing B2C user-flows is a matter of initiating authorization requests against the corresponding authorities. This sample demonstrates the sign-up/sign-in user-flow with self-service password reset.

Notable files and what they demonstrate

  1. ./src/App.js - Shows implementation of MsalProvider, all children will have access to @azure/msal-react context, hooks and components. Also shows how to handle password reset.
  2. ./src/index.js - Shows intialization of the PublicClientApplication that is passed to App.js
  3. ./src/pages/Home.jsx - Homepage, shows how to conditionally render content using AuthenticatedTemplate and UnauthenticatedTemplate depending on whether or not a user is signed in.
  4. ./src/pages/Profile.jsx - 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.
  5. ./src/authConfig.js - Configuration options for PublicClientApplication and token requests.
  6. ./src/ui-components/SignInSignOutButton.jsx - Example of how to conditionally render a Sign In or Sign Out button using the useIsAuthenticated hook.
  7. ./src/ui-components/SignInButton.jsx - Example of how to get the PublicClientApplication instance using the useMsal hook and invoking a login function.
  8. ./src/ui-components/SignOutButton.jsx - Example of how to get the PublicClientApplication instance using the useMsal hook and invoking a logout function.
  9. ./src/utils/MsGraphApiCall.js - Example of how to call the MS Graph API with an access token.

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

Installing @azure/msal-react and @azure/msal-browser from local builds
// 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/b2c-sample
// Build packages locally
npm run build:package
// Install sample dependencies
npm run install:local
// Install rest of dependencies
npm install

Note: If you suspect you are not using the local builds check that the package.json file shows the following dependencies:

"@azure/msal-react": "file:../../../lib/msal-react",
"@azure/msal-browser": "file:../../../lib/msal-browser",
"react": "file:../../../lib/msal-react/node_modules/react",
"react-dom": "file:../../../lib/msal-react/node_modules/react-dom",
Installing @azure/msal-react and @azure/msal-browser from released versions available on npm
// Change directory to sample directory
cd samples/msal-react-samples/b2c-sample
// Install packages from npm
npm run install:published
// Install rest of dependencies
npm install

Running the sample development server

  1. In a command prompt, run npm start.
  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 serve -s build
  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 the 3rd-party libraries used to create this sample

Setup webapi in Azure AD B2C

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published