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 Create React App.
./src/App.js- Shows implementation ofMsalProvider, all children will have access to@azure/msal-reactcontext, hooks and components../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../src/utils/NavigationClient.js- Example implementation ofINavigationClientwhich can be used to override the default navigation functions MSAL.js uses
For a demonstration of how to use MSAL React with class components, see: ./src/pages/ProfileWithMsal.jsx and ./src/pages/ProfileRawContext.jsx.
After you initialize MsalProvider, there are 3 approaches you can take to protect your class components with MSAL React:
- Wrap each component that you want to protect with
withMsalhigher-order component (HOC) (e.g. Profile). - Consume the raw context directly (e.g. ProfileContent).
- Pass context down from a parent component that has access to the
msalContextvia one of the other means above (e.g. ProfileContent).
For more information, visit:
- Ensure all pre-requisites have been completed to run
@azure/msal-react. - Install node.js if needed (https://nodejs.org/en/).
- Open
./env.developmentin an editor. - Replace
ENTER_CLIENT_ID_HEREwith the Application (client) ID from the portal registration, or use the currently configured lab registration. - Replace
ENTER_TENANT_ID_HEREwith the tenant 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.
These parameters are used in ./src/authConfig.js to configure MSAL.
# 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/react-router-sample
# Build packages locally
npm run build:package- 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.
@azure/msal-react and @azure/msal-browser support IE11 but the react-scripts package requires a few polyfills to work properly. In order to run this sample in IE11 go to src/index.js and uncomment the first 2 imports. We recommend using the redirect flow and setting the storeAuthStateInCookie config parameter to true in IE11 as there are known issues with popups. You can read more about the known issues with IE11 here