|
| 1 | +# MSAL.js for React Sample - Authorization Code Flow in Single-Page Applications |
| 2 | + |
| 3 | +## About this sample |
| 4 | + |
| 5 | +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. |
| 6 | +This sample was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). |
| 7 | + |
| 8 | +## Notable files and what they demonstrate |
| 9 | + |
| 10 | +1. `./src/App.js` - Shows implementation of `MsalProvider`, all children will have access to `@azure/msal-react` context, hooks and components. |
| 11 | +1. `./src/index.js` - Shows initialization of the `PublicClientApplication` that is passed to `App.js` |
| 12 | +1. `./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. |
| 13 | +1. `./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. |
| 14 | +1. `./src/authConfig.js` - Configuration options for `PublicClientApplication` and token requests. |
| 15 | +1. `./src/ui-components/SignInSignOutButton.jsx` - Example of how to conditionally render a Sign In or Sign Out button using the `useIsAuthenticated` hook. |
| 16 | +1. `./src/ui-components/SignInButton.jsx` - Example of how to get the `PublicClientApplication` instance using the `useMsal` hook and invoking a login function. |
| 17 | +1. `./src/ui-components/SignOutButton.jsx` - Example of how to get the `PublicClientApplication` instance using the `useMsal` hook and invoking a logout function. |
| 18 | +1. `./src/utils/MsGraphApiCall.js` - Example of how to call the MS Graph API with an access token. |
| 19 | +1. `./src/utils/NavigationClient.js` - Example implementation of `INavigationClient` which can be used to override the default navigation functions MSAL.js uses |
| 20 | + |
| 21 | +### (Optional) MSAL React and class components |
| 22 | + |
| 23 | +For a demonstration of how to use MSAL React with class components, see: `./src/pages/ProfileWithMsal.jsx` and `./src/pages/ProfileRawContext.jsx`. |
| 24 | + |
| 25 | +*After* you initialize `MsalProvider`, there are 3 approaches you can take to protect your class components with MSAL React: |
| 26 | + |
| 27 | +1. Wrap each component that you want to protect with `withMsal` higher-order component (HOC) (e.g. [Profile](./src/pages/ProfileWithMsal.jsx#Profile)). |
| 28 | +1. Consume the raw context directly (e.g. [ProfileContent](./src/pages/ProfileRawContext.jsx#ProfileContent)). |
| 29 | +1. Pass context down from a parent component that has access to the `msalContext` via one of the other means above (e.g. [ProfileContent](./src/pages/ProfileWithMsal.jsx#ProfileContent)). |
| 30 | + |
| 31 | +For more information, visit: |
| 32 | + |
| 33 | +- [Docs: Class Components](../../../lib/msal-react/docs/class-components.md) |
| 34 | +- [MSAL React FAQ](../../../lib/msal-react/FAQ.md) |
| 35 | + |
| 36 | +## How to run the sample |
| 37 | + |
| 38 | +### Pre-requisites |
| 39 | + |
| 40 | +- Ensure [all pre-requisites](../../../lib/msal-react/README.md#prerequisites) have been completed to run `@azure/msal-react`. |
| 41 | +- Install node.js if needed (<https://nodejs.org/en/>). |
| 42 | + |
| 43 | +### Configure the application |
| 44 | + |
| 45 | +- Open `./src/authConfig.js` in an editor. |
| 46 | +- Replace client id with the Application (client) ID from the portal registration, or use the currently configured lab registration. |
| 47 | + - Optionally, you may replace any of the other parameters, or you can remove them and use the default values. |
| 48 | + |
| 49 | +#### Install npm dependencies for sample |
| 50 | + |
| 51 | +##### Installing @azure/msal-react and @azure/msal-browser from local builds |
| 52 | + |
| 53 | +```bash |
| 54 | +# Install dev dependencies for msal-react and msal-browser from root of repo |
| 55 | +npm install |
| 56 | + |
| 57 | +# Change directory to sample directory |
| 58 | +cd samples/msal-react-samples/react-18-sample |
| 59 | + |
| 60 | +# Build packages locally |
| 61 | +npm run build:package |
| 62 | + |
| 63 | +# Install local libs |
| 64 | +npm run install:local |
| 65 | + |
| 66 | +# Install sample dependencies |
| 67 | +npm install |
| 68 | +``` |
| 69 | + |
| 70 | +Note: If you suspect you are not using the local builds check that the `package.json` file shows the following dependencies: |
| 71 | + |
| 72 | +``` |
| 73 | +"@azure/msal-react": "file:../../../lib/msal-react", |
| 74 | +"@azure/msal-browser": "file:../../../lib/msal-browser", |
| 75 | +"react": "file:../../../lib/msal-react/node_modules/react", |
| 76 | +"react-dom": "file:../../../lib/msal-react/node_modules/react-dom", |
| 77 | +``` |
| 78 | + |
| 79 | +##### Installing @azure/msal-react and @azure/msal-browser from released versions available on npm |
| 80 | + |
| 81 | +```bash |
| 82 | +# Change directory to sample directory |
| 83 | +cd samples/msal-react-samples/react-18-sample |
| 84 | + |
| 85 | +# Install packages from npm |
| 86 | +npm run install:published |
| 87 | + |
| 88 | +# Install rest of dependencies |
| 89 | +npm install |
| 90 | +``` |
| 91 | + |
| 92 | +#### Running the sample development server |
| 93 | + |
| 94 | +1. In a command prompt, run `npm start`. |
| 95 | +1. Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
| 96 | +1. Open [http://localhost:3000/profile](http://localhost:3000/profile) to see an example of a protected route. If you are not yet signed in, signin will be invoked automatically. |
| 97 | + |
| 98 | +The page will reload if you make edits. |
| 99 | +You will also see any lint errors in the console. |
| 100 | + |
| 101 | +- 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. |
| 102 | + |
| 103 | +#### Running the sample production server |
| 104 | + |
| 105 | +1. In a command prompt, run `npm run build`. |
| 106 | +1. Next run `serve -s build` |
| 107 | +1. Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
| 108 | +1. Open [http://localhost:3000/profile](http://localhost:3000/profile) to see an example of a protected route. If you are not yet signed in, signin will be invoked automatically. |
| 109 | + |
| 110 | +#### Learn more about the 3rd-party libraries used to create this sample |
| 111 | + |
| 112 | +- [React documentation](https://reactjs.org/). |
| 113 | +- [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started) |
| 114 | +- [React Router documentation](https://reactrouterdotcom.fly.dev/docs/en/v6) |
| 115 | +- [Material-UI documentation](https://mui.com/getting-started/installation/) |
0 commit comments