This developer sample is used to run basic B2C use cases (user-flows) 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-react
context, hooks and components. Also shows how to handle edit profile user-flow../src/index.js
- Shows intialization of thePublicClientApplication
that is passed toApp.js
./src/pages/Home.jsx
- Homepage, shows how to conditionally render content usingAuthenticatedTemplate
andUnauthenticatedTemplate
depending 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 forPublicClientApplication
and B2C policies../src/ui-components/SignInSignOutButton.jsx
- Example of how to conditionally render a Sign In or Sign Out button using theuseIsAuthenticated
hook../src/ui-components/SignInButton.jsx
- Example of how to get thePublicClientApplication
instance using theuseMsal
hook and invoking a login function../src/ui-components/SignOutButton.jsx
- Example of how to get thePublicClientApplication
instance using theuseMsal
hook 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/).
This sample comes with a pre-registered application for demo purposes. If you would like to use your own Azure AD B2C tenant and application, follow the steps below:
- Create an Azure Active Directory B2C tenant
- Register a single-page application in Azure Active Directory B2C
- Create user-flows in Azure Active Directory B2C
- Open
./src/authConfig.js
in an editor. - Replace
clientId
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.
- Replace
names
,authorities
andauthorityDomain
fields inb2cPolicies
object with the parameters you've obtained after creating your own user-flows.- Optionally, replace the
uri
andscopes
fields inapiConfig
object if you would like to call your own web API registered on Azure AD B2C (see: Register a web API on Azure AD B2C)
- Optionally, replace the
These parameters are taken in during runtime to initialize MSAL in ./src/index.js
.
// 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
- In a command prompt, run
npm start
. - Open http://localhost:4200 to view it in the browser.
- Open http://localhost:4200/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
orSign in using Redirect
to begin the auth flow.
- In a command prompt, run
npm run build
. - Next run
serve -s build
- Open http://localhost:4200 to view it in the browser.
- Open http://localhost:4200/profile to see an example of a protected route. If you are not yet signed in, signin will be invoked automatically.