|
1 | 1 | ---
|
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 |
5 | 5 | author: craigshoemaker
|
6 | 6 | ms.service: azure-functions
|
7 | 7 | ms.topic: conceptual
|
8 | 8 | ms.date: 05/08/2020
|
9 | 9 | ms.author: cshoe
|
10 | 10 | ---
|
11 | 11 |
|
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 |
13 | 13 |
|
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). |
15 | 15 |
|
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. |
17 | 17 |
|
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 |
21 | 19 |
|
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: |
23 | 21 |
|
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). | |
25 | 28 |
|
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: |
32 | 30 |
|
33 |
| -## <section> |
| 31 | +```json |
| 32 | +{ |
| 33 | + "identityProvider": "facebook", |
| 34 | + "userId": "d75b260a64504067bfc5b2905e3b8182", |
| 35 | + "userDetails": "[email protected]", |
| 36 | + "userRoles": [ "anonymous", "authenticated" ] |
| 37 | +} |
| 38 | +``` |
34 | 39 |
|
35 |
| -<!---Detail what the reader needs to know in each section |
36 |
| ----> |
| 40 | +## Direct-access endpoint |
37 | 41 |
|
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. |
39 | 43 |
|
40 |
| -1. Step one of the procedure |
41 |
| -1. Step two of the procedure |
42 |
| -1. Step three of the procedure |
43 |
| - <!---  ---> |
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`. |
47 | 45 |
|
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. |
49 | 47 |
|
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 | +} |
51 | 55 |
|
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 | +``` |
54 | 78 |
|
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. |
56 | 80 |
|
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