C Msal4
C Msal4
C Msal4
---
---
1. Open your terminal and run the following command to create a new React app:
```bash
npx create-react-app my-msal-app
cd my-msal-app
```
2. Once the app is created, you can open it in your preferred code editor.
---
---
---
1. In your React app, create a new folder called `auth` inside the `src` directory:
```
src/
└── auth/
```
2. Inside the `auth` folder, create a new file named `msalConfig.js` and add the following
configuration:
```javascript
import { PublicClientApplication } from "@azure/msal-browser";
const msalConfig = {
auth: {
clientId: "YOUR_CLIENT_ID", // Replace with your client ID
authority: "https://login.microsoftonline.com/YOUR_TENANT_ID", // Replace with
your tenant ID
redirectUri: "http://localhost:3000", // Your redirect URI
},
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you have issues on IE11 or Edge
}
};
---
2. **AuthProvider.js:**
```javascript
import React from "react";
import { MsalProvider } from "@azure/msal-react";
import msalInstance from "./msalConfig";
3. **SignInButton.js:**
```javascript
import React from "react";
import { useMsal } from "@azure/msal-react";
---
1. Open `src/index.js` and wrap the `<App />` component with the `AuthProvider`:
```javascript
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import AuthProvider from "./auth/AuthProvider";
ReactDOM.render(
<React.StrictMode>
<AuthProvider>
<App />
</AuthProvider>
</React.StrictMode>,
document.getElementById("root")
);
```
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Welcome to MSAL React App</h1>
<SignInButton />
</header>
</div>
);
}
---
2. Open your browser and navigate to `http://localhost:3000`. You should see a simple page
with a "Sign In" button.
3. Click the "Sign In" button to initiate the Microsoft login process. After successful
authentication, you should be redirected back to your app.
---
After completing the above steps, your folder structure should look something like this:
```
my-msal-app/
├── node_modules/
├── public/
├── src/
│ ├── auth/
│ │ ├── AuthProvider.js
│ │ ├── msalConfig.js
│ │ └── SignInButton.js
│ ├── App.css
│ ├── App.js
│ ├── index.css
│ └── index.js
├── .gitignore
├── package.json
├── README.md
└── yarn.lock (or package-lock.json)
```
---
This guide should provide you with a clear path to integrating MSAL into your React
application.