Skip to content

Commit 3da14f4

Browse files
Updates after review feedback
1 parent 76f0b9b commit 3da14f4

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

articles/static-apps/user-information.md

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@ ms.author: cshoe
1111

1212
# Accessing user information in Azure Static Web Apps
1313

14-
Azure Static Web Apps provides authentication-related user information via a [direct request URL](#direct-access) and to [API](#api-access) functions.
14+
Azure Static Web Apps provides authentication-related user information via a [direct access endpoint](#direct-access-endpoint) and to [API functions](#api-functions).
15+
16+
Many user interfaces rely heavily on user authentication data. The direct access endpoint is a helper that exposes user information without having to implement a custom function. Beyond convenience, the direct access endpoint is not subject to cold start delays associated with serverless architecture.
1517

1618
## Client principal data
1719

18-
User information is available in the app via the `x-ms-client-principal` request header. The client principal data is sent as a Base64-encoded string that contains a serialized JSON object. The object contains the following properties:
20+
User information is available in the app via the `x-ms-client-principal` request header. The client principal data is sent as a [Base64](https://www.wikipedia.org/wiki/Base64)-encoded string containing a serialized JSON object.
21+
22+
The following properties are featured in the client principal object:
1923

2024
| Property | Description |
2125
|-----------|---------|
2226
| `identityProvider` | The name of the [identity provider](authentication-authorization.md). |
23-
| `userId` | An Azure Static Web Apps-specific unique identifier for the user. <ul><li>The value is unique on a per-app basis. For instance, the same user returns a different `userId` value on a different Static Web App.<li>The value persists for the lifetime of a user. If you delete and then add the same user back to the app, then the `userId` is a different value.</ul>|
27+
| `userId` | An Azure Static Web Apps-specific unique identifier for the user. <ul><li>The value is unique on a per-app basis. For instance, the same user returns a different `userId` value on a different Static Web Apps site.<li>The value persists for the lifetime of a user. If you delete and add the same user back to the app, a new `userId` is generated.</ul>|
2428
| `userDetails` | Username or email address of the user. Some providers return the [user's email address](authentication-authorization.md), while others send the [user handle](authentication-authorization.md). |
2529
| `userRoles` | An array of the [user's assigned roles](authentication-authorization.md). |
2630

27-
The following example is a sample decoded `x-ms-client-principal` payload:
31+
The following example is a sample decoded `x-ms-client-principal` object:
2832

2933
```json
3034
{
@@ -35,39 +39,28 @@ The following example is a sample decoded `x-ms-client-principal` payload:
3539
}
3640
```
3741

38-
## Direct access
42+
## Direct access endpoint
3943

40-
You can send a `GET` request to the `/.auth/me` route and receive direct access to the client principal data. When the state of your view is driven by authorization, use this approach for the best performance.
44+
You can send a `GET` request to the `/.auth/me` route and receive direct access to the client principal data. When the state of your view is relies on authorization data, use this approach for the best performance.
4145

42-
When users are logged-in, the payload from this request is a client principal JSON object. Requests from unauthenticated users returns `null`.
46+
For logged-in users, the response contains a client principal JSON object. Requests from unauthenticated users returns `null`.
4347

44-
Using the [fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch) API, you can access the client principal data using the following syntax.
48+
Using the [fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch)\* API, you can access the client principal data using the following syntax.
4549

4650
```javascript
47-
fetch("/.auth/me/")
48-
.then(response => response.json())
49-
.then(payload => {
50-
const { clientPrincipal } = payload;
51-
console.log(clientPrincipal);
52-
});
53-
```
54-
55-
Libraries like [axios](https://github.com/axios/axios) make accessing user information even easier.
51+
async function getUserInfo() {
52+
const response = await fetch("/api/user");
53+
const payload = await response.json();
54+
const { clientPrincipal } = payload;
55+
return clientPrincipal;
56+
}
5657

57-
```javascript
58-
axios.get("/.auth/me").then(response => {
59-
const { clientPrincipal } = response.data;
60-
console.log(clientPrincipal);
61-
});
58+
console.log(getUserInfo());
6259
```
6360

64-
## API access
65-
66-
Client principal data is passed to API functions in the request header. To make this information accessible to the browser, you can return user data from a function.
61+
## API functions
6762

68-
### API access in the function
69-
70-
The following example function, named `user`, shows how to return user information to the client.
63+
Client principal data is passed to API functions in the request header. The following example function, named `user`, shows how to read and return user information.
7164

7265
```javascript
7366
module.exports = async function (context, req) {
@@ -83,15 +76,20 @@ module.exports = async function (context, req) {
8376
};
8477
```
8578

86-
### In the browser
87-
88-
Using the [fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch) API, you can access the API's response using the following syntax.
79+
Using the [fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch)\* browser API, you can access the API's response using the following syntax.
8980

9081
```javascript
91-
const response = await fetch("/api/user/");
92-
const payload = await response.json();
93-
const { clientPrincipal } = payload;
94-
console.log(clientPrincipal);
82+
async function getUser() {
83+
const response = await fetch("/api/user");
84+
const payload = await response.json();
85+
const { clientPrincipal } = payload;
86+
return clientPrincipal;
87+
}
88+
89+
console.log(getUser());
90+
```
91+
92+
\* The [fetch](https://caniuse.com/#feat=fetch) API and [await](https://caniuse.com/#feat=mdn-javascript_operators_await) operator are not supported in Internet Explorer.
9593

9694
## Next steps
9795

0 commit comments

Comments
 (0)