Skip to content

Commit 95465a8

Browse files
tnorlingjo-arroyo
andauthored
Add sample for React 18 (#4546)
* Support React 18 * react 18 sample * Change files * Update samples/msal-react-samples/react-18-sample/README.md Co-authored-by: Jo Arroyo <joarroyo@microsoft.com> Co-authored-by: Jo Arroyo <joarroyo@microsoft.com>
1 parent 193aad5 commit 95465a8

36 files changed

Lines changed: 1205 additions & 2 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Add react 18 as supported peer dependency #4546",
4+
"packageName": "@azure/msal-react",
5+
"email": "thomas.norling@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

lib/msal-react/FAQ.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Please see [here](https://github.com/AzureAD/microsoft-authentication-library-fo
3333

3434
### What versions of React are supported?
3535

36-
React versions 16 and 17 are supported.
36+
React versions 16.8.0+, 17 and 18 are supported.
3737

3838
### Does `@azure/msal-react` support Server Side Rendering (SSR) or static site generation?
3939

lib/msal-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
},
4343
"peerDependencies": {
4444
"@azure/msal-browser": "^2.22.0",
45-
"react": "^16.8.0 || ^17"
45+
"react": "^16.8.0 || ^17 || ^18"
4646
},
4747
"module": "dist/msal-react.esm.js",
4848
"devDependencies": {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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/)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "react-18-sample",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@azure/msal-browser": "^2.21.0",
7+
"@azure/msal-react": "^1.2.0",
8+
"@emotion/react": "^11.7.1",
9+
"@emotion/styled": "^11.6.0",
10+
"@mui/icons-material": "^5.3.1",
11+
"@mui/material": "^5.4.0",
12+
"@mui/styles": "^5.3.0",
13+
"react": "^18.0.0-rc.0",
14+
"react-dom": "^18.0.0-rc.0",
15+
"react-router-dom": "^6.2.1",
16+
"react-scripts": "5.0.0"
17+
},
18+
"scripts": {
19+
"start": "react-scripts start",
20+
"build": "react-scripts build",
21+
"build:package": "cd ../../../lib/msal-react & npm run build:all",
22+
"install:local": "npm install ../../../lib/msal-react ../../../lib/msal-react/node_modules/react ../../../lib/msal-react/node_modules/react-dom ../../../lib/msal-browser",
23+
"install:published": "npm install @azure/msal-browser@latest @azure/msal-react@latest react@^18 react-dom@^18"
24+
},
25+
"eslintConfig": {
26+
"extends": [
27+
"react-app"
28+
]
29+
},
30+
"browserslist": {
31+
"production": [
32+
">0.2%",
33+
"not dead",
34+
"not op_mini all"
35+
],
36+
"development": [
37+
"last 1 chrome version",
38+
"last 1 firefox version",
39+
"last 1 safari version"
40+
]
41+
}
42+
}
3.78 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>
5.22 KB
Loading

0 commit comments

Comments
 (0)