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/static-apps/user-information.md
+33-35Lines changed: 33 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,20 +11,24 @@ ms.author: cshoe
11
11
12
12
# Accessing user information in Azure Static Web Apps
13
13
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.
15
17
16
18
## Client principal data
17
19
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:
19
23
20
24
| Property | Description |
21
25
|-----------|---------|
22
26
|`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>|
24
28
|`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). |
25
29
|`userRoles`| An array of the [user's assigned roles](authentication-authorization.md). |
26
30
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:
28
32
29
33
```json
30
34
{
@@ -35,39 +39,28 @@ The following example is a sample decoded `x-ms-client-principal` payload:
35
39
}
36
40
```
37
41
38
-
## Direct access
42
+
## Direct access endpoint
39
43
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.
41
45
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`.
43
47
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.
45
49
46
50
```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
+
asyncfunctiongetUserInfo() {
52
+
constresponse=awaitfetch("/api/user");
53
+
constpayload=awaitresponse.json();
54
+
const { clientPrincipal } = payload;
55
+
return clientPrincipal;
56
+
}
56
57
57
-
```javascript
58
-
axios.get("/.auth/me").then(response=> {
59
-
const { clientPrincipal } =response.data;
60
-
console.log(clientPrincipal);
61
-
});
58
+
console.log(getUserInfo());
62
59
```
63
60
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
67
62
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.
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.
89
80
90
81
```javascript
91
-
constresponse=awaitfetch("/api/user/");
92
-
constpayload=awaitresponse.json();
93
-
const { clientPrincipal } = payload;
94
-
console.log(clientPrincipal);
82
+
asyncfunctiongetUser() {
83
+
constresponse=awaitfetch("/api/user");
84
+
constpayload=awaitresponse.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.
0 commit comments