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.
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.
./src/App.js- Shows implementation ofMsalProvider, all children will have access to@azure/msal-reactcontext, hooks and components. Also shows how to handle password reset../src/index.js- Shows intialization of thePublicClientApplicationthat is passed toApp.js./src/pages/Home.jsx- Homepage, shows how to conditionally render content usingAuthenticatedTemplateandUnauthenticatedTemplatedepending on whether or not a user is signed in../src/pages/Profile.jsx- Example of a protected route usingMsalAuthenticationTemplate. 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../src/authConfig.js- Configuration options forPublicClientApplicationand token requests../src/ui-components/SignInSignOutButton.jsx- Example of how to conditionally render a Sign In or Sign Out button using theuseIsAuthenticatedhook../src/ui-components/SignInButton.jsx- Example of how to get thePublicClientApplicationinstance using theuseMsalhook and invoking a login function../src/ui-components/SignOutButton.jsx- Example of how to get thePublicClientApplicationinstance using theuseMsalhook and invoking a logout function../src/utils/MsGraphApiCall.js- Example of how to call the MS Graph API with an access token.
- Ensure all pre-requisites have been completed to run
@azure/msal-react. - Install node.js if needed (https://nodejs.org/en/).
- Open
./src/authConfig.jsin 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 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 installNote: 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",
// 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- In a command prompt, run
npm start. - Open http://localhost:3000 to view it in the browser.
- 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 PopuporSign in using Redirectto begin the auth flow.
- In a command prompt, run
npm run build. - Next run
serve -s build - Open http://localhost:3000 to view it in the browser.
- 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.