Skip to content

Commit c5d4170

Browse files
Merge pull request #111300 from craigshoemaker/marmalade-user-info
[New Article] Add accessing user info article
2 parents adcb9aa + 98968d2 commit c5d4170

File tree

2 files changed

+76
-38
lines changed

2 files changed

+76
-38
lines changed

articles/static-apps/TOC.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
href: routes.md
1818
- name: Authentication and authorization
1919
href: authentication-authorization.md
20-
- name: Accessing user information
20+
- name: Access user information
2121
href: user-information.md
22-
- name: Review pull requests in preproduction environments
22+
- name: Review pull requests in pre-production environments
2323
href: review-publish-pull-requests.md
2424
- name: Custom domain
2525
href: custom-domain.md
Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,97 @@
11
---
2-
title: Accessing user information in an App Service Static Apps API
3-
description: #Required; article description that is displayed in search results.
4-
services: #Required for articles that deal with a service; service slug assigned to your service by ACOM.
2+
title: Accessing user information in Azure Static Web Apps
3+
description: Learn to read authorization provider-returned user data.
4+
services: azure-functions
55
author: craigshoemaker
66
ms.service: azure-functions
77
ms.topic: conceptual
88
ms.date: 05/08/2020
99
ms.author: cshoe
1010
---
1111

12-
<!---Recommended: Removal all the comments in this template before you sign-off or merge to master.--->
12+
# Accessing user information in Azure Static Web Apps
1313

14-
# Accessing user information in an App Service Static Apps API
14+
Azure Static Web Apps provides authentication-related user information via a [direct-access endpoint](#direct-access-endpoint) and to [API functions](#api-functions).
1515

16-
Introductory paragraph.
16+
Many user interfaces rely heavily on user authentication data. The direct-access endpoint is a utility API that exposes user information without having to implement a custom function. Beyond convenience, the direct-access endpoint isn't subject to cold start delays that are associated with serverless architecture.
1717

18-
<!---Required:
19-
Lead with a light intro that describes, in customer-friendly language, what the customer will learn, or do, or accomplish. Answer the fundamental "why would I want to do this?" question.
20-
--->
18+
## Client principal data
2119

22-
<!---Avoid notes, tips, and important boxes. Readers tend to skip over them. Better to put that info directly into the article text.--->
20+
Client principal data object exposes user-identifiable information to your app. The following properties are featured in the client principal object:
2321

24-
## Prerequisites
22+
| Property | Description |
23+
|-----------|---------|
24+
| `identityProvider` | The name of the [identity provider](authentication-authorization.md). |
25+
| `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 resource.<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>|
26+
| `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). |
27+
| `userRoles` | An array of the [user's assigned roles](authentication-authorization.md). |
2528

26-
- First prerequisite
27-
- Second prerequisite
28-
- Third prerequisite
29-
<!--- Make Prerequisites your first H2 if you have them.
30-
If there's something a customer needs to take care of before they start (for example, creating a VM) it's OK to link to that content before they begin.
31-
--->
29+
The following example is a sample client principal object:
3230

33-
## <section>
31+
```json
32+
{
33+
"identityProvider": "facebook",
34+
"userId": "d75b260a64504067bfc5b2905e3b8182",
35+
"userDetails": "[email protected]",
36+
"userRoles": [ "anonymous", "authenticated" ]
37+
}
38+
```
3439

35-
<!---Detail what the reader needs to know in each section
36-
--->
40+
## Direct-access endpoint
3741

38-
Include a sentence or two to explain only what is needed to complete the procedure.
42+
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 relies on authorization data, use this approach for the best performance.
3943

40-
1. Step one of the procedure
41-
1. Step two of the procedure
42-
1. Step three of the procedure
43-
<!--- ![Browser](media/contribute-how-to-mvc-quickstart/browser.png) --->
44-
<!---Use screenshots but be judicious to maintain a reasonable length. Make sure screenshots align to the [current standards](contribute-mvc-screen-shots.md).
45-
If users access your product/service via a web browser the first screenshot should always include the full browser window in Chrome or Safari. This is to show users that the portal is browser-based - OS and browser agnostic.--->
46-
1. Step four of the procedure
44+
For logged-in users, the response contains a client principal JSON object. Requests from unauthenticated users returns `null`.
4745

48-
## Next steps
46+
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.
4947

50-
<!-- Uncomment this block and add the appropriate link
48+
```javascript
49+
async function getUserInfo() {
50+
const response = await fetch("/.auth/me");
51+
const payload = await response.json();
52+
const { clientPrincipal } = payload;
53+
return clientPrincipal;
54+
}
5155

52-
> [!div class="nextstepaction"]
53-
> [Next steps button](contribute-get-started-mvc.md)
56+
console.log(getUserInfo());
57+
```
58+
59+
## API functions
60+
61+
Client principal data is passed to API functions in 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.
62+
63+
The following example function, named `user`, shows how to read and return user information.
64+
65+
```javascript
66+
module.exports = async function (context, req) {
67+
const header = req.headers["x-ms-client-principal"];
68+
const encoded = Buffer.from(header, "base64");
69+
const decoded = encoded.toString("ascii");
70+
71+
context.res = {
72+
body: {
73+
clientPrincipal: JSON.parse(decoded)
74+
}
75+
};
76+
};
77+
```
5478

55-
-->
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.
5680

57-
<!--- Required:
58-
A single link in the blue box format should direct the customer to the next article - and you can shorten the title in the boxes if the original one doesn't fit.
59-
--->
81+
```javascript
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 aren't supported in Internet Explorer.
93+
94+
## Next steps
95+
96+
> [!div class="nextstepaction"]
97+
> [Configure environment variables](environment-variables.md)

0 commit comments

Comments
 (0)