You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/active-directory/develop/tutorial-single-page-app-react-call-api.md
+68-81Lines changed: 68 additions & 81 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,28 +33,15 @@ In this tutorial, you learn how to:
33
33
34
34
To allow the SPA to request access to Microsoft Graph, a reference to the `graphConfig` object needs to be added. This contains the Graph REST API endpoint defined in *authConfig.js* file.
35
35
36
-
### [Visual Studio](#tab/visual-studio)
37
-
38
-
1. Right click on the *src* folder, select **Add** > **New Item**. Create a new file called *graph.js* and select **Add**.
39
-
1. Replace the contents of the file with the following code snippet to request access to Microsoft Graph;
36
+
1. In the *src* folder, open *graph.js* and replace the contents of the file with the following code snippet to request access to Microsoft Graph.
By default, the application runs via a JavaScript file called *App.js*. It needs to be changed to *App.jsx* file, which is an extension that allows a developer to write HTML in React.
55
43
56
-
1. Rename *App.js* to *App.jsx*.
57
-
1. Replace the existing imports with the following snippet;
44
+
1. In the *src* folder, open *App.jsx* and replace the contents of the file with the following code snippet to request access.
58
45
59
46
```javascript
60
47
importReact, { useState } from'react';
@@ -75,78 +62,78 @@ By default, the application runs via a JavaScript file called *App.js*. It needs
75
62
76
63
The `ProfileContent` function is used to render the user's profile information. In the *App.jsx* file, add the following code below your imports:
77
64
78
-
```javascript
79
-
80
-
/**
81
-
* Renders information about the signed-in user or a button to retrieve data about the user
82
-
*/
83
-
constProfileContent= () => {
84
-
const { instance, accounts } =useMsal();
85
-
const [graphData, setGraphData] =useState(null);
86
-
87
-
functionRequestProfileData() {
88
-
// Silently acquires an access token which is then attached to a request for MS Graph data
### Replacing the default function to render authenticated information
116
103
117
104
The following code will render based on whether the user is authenticated or not. Replace the default function `App()` to render authenticated information with the following code:
118
105
119
-
```javascript
120
-
/**
121
-
* If a user is authenticated the ProfileContent component above is rendered. Otherwise a message indicating a user is not authenticated is rendered.
122
-
*/
123
-
constMainContent= () => {
124
-
return (
125
-
<div className="App">
126
-
<AuthenticatedTemplate>
127
-
<ProfileContent />
128
-
</AuthenticatedTemplate>
129
-
130
-
<UnauthenticatedTemplate>
131
-
<h5>
132
-
<center>
133
-
Please sign-in to see your profile information.
134
-
</center>
135
-
</h5>
136
-
</UnauthenticatedTemplate>
137
-
</div>
138
-
);
139
-
};
140
-
141
-
exportdefaultfunctionApp() {
142
-
return (
143
-
<PageLayout>
144
-
<center>
145
-
<MainContent />
146
-
</center>
147
-
</PageLayout>
148
-
);
149
-
}
106
+
```javascript
107
+
/**
108
+
* If a user is authenticated the ProfileContent component above is rendered. Otherwise a message indicating a user is not authenticated is rendered.
1. Replace the following values with the values from the Microsoft Entra admin center.
97
-
- `clientId` - The identifier of the application, also referred to as the client. Replace `Enter_the_Application_Id_Here` with the **Application (client) ID** value that was recorded earlier from the overview page of the registered application.
98
-
- `authority` - This is composed of two parts:
99
-
- The *Instance* is endpoint of the cloud provider. Check with the different available endpoints in [National clouds](authentication-national-cloud.md#azure-ad-authentication-endpoints).
100
-
- The *Tenant ID* is the identifier of the tenant where the application is registered. Replace the `_Enter_the_Tenant_Info_Here` with the **Directory (tenant) ID** value that was recorded earlier from the overview page of the registered application.
101
-
102
-
### [Visual Studio Code](#tab/visual-studio-code)
103
-
104
-
1. In the *src* folder, create a new file called *authConfig.js*.
105
-
1. Open *authConfig.js* and add the following code snippet:
108
+
1. In the *src* folder, open *authConfig.js* and add the following code snippet:
@@ -112,16 +115,19 @@ To learn more about these packages refer to the documentation in [msal-browser](
112
115
- The *Instance* is endpoint of the cloud provider. Check with the different available endpoints in [National clouds](authentication-national-cloud.md#azure-ad-authentication-endpoints).
113
116
- The *Tenant ID* is the identifier of the tenant where the application is registered. Replace the `_Enter_the_Tenant_Info_Here` with the **Directory (tenant) ID** value that was recorded earlier from the overview page of the registered application.
114
117
118
+
1. Save the file.
115
119
---
116
120
117
121
## Modify *index.js* to include the authentication provider
118
122
119
123
All parts of the app that require authentication must be wrapped in the [`MsalProvider`](/javascript/api/@azure/msal-react/#@azure-msal-react-msalprovider) component. You instantiate a [PublicClientApplication](/javascript/api/@azure/msal-browser/publicclientapplication) then pass it to `MsalProvider`.
120
124
121
-
1. In the *src* folder, open the *index.js* file and replace the contents of the file with the following code snippet to use the `msal` packages and bootstrap styling:
125
+
1. In the *src* folder, open *index.js* and replace the contents of the file with the following code snippet to use the `msal` packages and bootstrap styling:
Copy file name to clipboardExpand all lines: articles/active-directory/develop/tutorial-single-page-app-react-sign-in-users.md
+4-43Lines changed: 4 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,48 +30,7 @@ In this tutorial:
30
30
31
31
* Completion of the prerequisites and steps in [Tutorial: Prepare an application for authentication](tutorial-single-page-app-react-prepare-spa.md).
32
32
33
-
## Adding components to the application
34
-
35
-
The project needs extra files to be created for the page layout, displaying profile data, sign in and sign out options.
36
-
37
-
### [Visual Studio](#tab/visual-studio)
38
-
39
-
1. In the Solution Explorer, open the *src* folder.
40
-
1. Select **Add** > **New Folder** and name it *components*.
41
-
1. Open the *components* folder, and select **Add** > **New Item**.
42
-
1. Search for and select **JSX file**, and create the following four files:
43
-
-*PageLayout.jsx*
44
-
-*ProfileData.jsx*
45
-
-*SignInButton.jsx*
46
-
-*SignOutButton.jsx*
47
-
48
-
### [Visual Studio Code](#tab/visual-studio-code)
49
-
50
-
1. Navigate to the *src* folder in the left panel.
51
-
1. Right click on *src*, select **New Folder** and call it *components*.
52
-
1. Right click on *components* and using the **New File** option, create the following four files;
53
-
-*PageLayout.jsx*
54
-
-*ProfileData.jsx*
55
-
-*SignInButton.jsx*
56
-
-*SignOutButton.jsx*
57
-
58
-
---
59
-
60
-
Once complete, you should have the following folder structure.
61
-
62
-
```txt
63
-
reactspalocal/
64
-
├── src/
65
-
│ ├── components/
66
-
│ │ ├── PageLayout.jsx
67
-
│ │ ├── ProfileData.jsx
68
-
│ │ ├── SignInButton.jsx
69
-
│ │ └── SignOutButton.jsx
70
-
│ └── ...
71
-
└── ...
72
-
```
73
-
74
-
### Adding the page layout
33
+
### Adding the page layout component
75
34
76
35
1. Open *PageLayout.jsx* and add the following code to render the page layout. The [useIsAuthenticated](/javascript/api/@azure/msal-react) hook returns whether or not a user is currently signed-in.
77
36
@@ -123,7 +82,7 @@ reactspalocal/
123
82
124
83
1. Save the file.
125
84
126
-
### Display profile information
85
+
### Display profile information
127
86
128
87
1. Open the *ProfileData.jsx* and add the following code, which creates a component that displays the user's profile information:
129
88
@@ -254,5 +213,7 @@ reactspalocal/
254
213
255
214
1. Save the file.
256
215
216
+
## Next steps
217
+
257
218
> [!div class="nextstepaction"]
258
219
> [Tutorial: Call an API from a React single-page app](tutorial-single-page-app-react-call-api.md)
0 commit comments