Skip to content

Commit 7bd1737

Browse files
authored
Merge pull request #115294 from mmacy/msid-spa-auth-code-appreg-protocol
[msid] SPA app reg w/auth code & MSAL.js 2.0 (preview)
2 parents 4e5fb82 + a30ce08 commit 7bd1737

16 files changed

+551
-352
lines changed

articles/active-directory/develop/TOC.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
items:
2929
- name: Angular
3030
href: quickstart-v2-angular.md
31-
- name: JavaScript
31+
- name: JavaScript - Auth code flow
32+
href: quickstart-v2-javascript-auth-code.md
33+
- name: JavaScript - Implicit flow
3234
href: quickstart-v2-javascript.md
3335
- name: Web apps
3436
items:
@@ -73,7 +75,9 @@
7375
items:
7476
- name: Angular
7577
href: tutorial-v2-angular.md
76-
- name: JavaScript
78+
- name: JavaScript - Auth code flow
79+
href: tutorial-v2-javascript-auth-code.md
80+
- name: JavaScript - Implicit flow
7781
href: tutorial-v2-javascript-spa.md
7882
- name: Web apps
7983
items:
@@ -479,6 +483,10 @@
479483
href: active-directory-optional-claims.md
480484
- name: Configure token lifetimes
481485
href: active-directory-configurable-token-lifetimes.md
486+
- name: Handle SameSite cookie changes in Chrome browser
487+
href: howto-handle-samesite-cookie-changes-chrome-browser.md
488+
- name: Handle ITP in Safari
489+
href: reference-third-party-cookies-spas.md
482490
- name: Application configuration
483491
displayName: App configuration
484492
items:

articles/active-directory/develop/index.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ landingContent:
4545
url: scenario-spa-overview.md
4646
- linkListType: quickstart
4747
links:
48-
- text: JavaScript
48+
- text: JavaScript - Auth code
49+
url: quickstart-v2-javascript-auth-code.md
50+
- text: JavaScript - Implicit
4951
url: quickstart-v2-javascript.md
5052
- text: Angular
5153
url: quickstart-v2-angular.md
5254
- linkListType: tutorial
5355
links:
54-
- text: JavaScript
56+
- text: JavaScript - Auth code
57+
url: tutorial-v2-javascript-auth-code.md
58+
- text: JavaScript - Implicit
5559
url: tutorial-v2-javascript-spa.md
5660
- linkListType: download
5761
links:
76.9 KB
Loading
76.9 KB
Loading
48.9 KB
Loading
55 KB
Loading
32.6 KB
Loading
38.7 KB
Loading
33.5 KB
Loading
Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Sign in users in JavaScript single-page apps with auth code | Azure
2+
title: Sign in users in JavaScript single-page apps (SPA) with auth code | Azure
33
titleSuffix: Microsoft identity platform
44
description: Learn how a JavaScript app can call an API that requires access tokens using the Microsoft identity platform.
55
services: active-directory
@@ -10,29 +10,26 @@ ms.service: active-directory
1010
ms.subservice: develop
1111
ms.topic: quickstart
1212
ms.workload: identity
13-
ms.date: 04/22/2020
13+
ms.date: 05/19/2020
1414
ms.author: hahamil
15-
ms.custom: aaddev, identityplatformtop40, scenarios:getting-started, languages:JavaScript
16-
ROBOTS: NOINDEX
15+
ms.custom: aaddev, scenarios:getting-started, languages:JavaScript
1716
#Customer intent: As an app developer, I want to learn how to get access tokens and refresh tokens by using the Microsoft identity platform endpoint so that my JavaScript app can sign in users of personal accounts, work accounts, and school accounts.
1817
---
1918

20-
# Quickstart: Sign in users and get an access token in a JavaScript SPA using the Auth Code Flow
19+
# Quickstart: Sign in users and get an access token in a JavaScript SPA using the auth code flow
2120

2221
> [!IMPORTANT]
2322
> This feature is currently in preview. Previews are made available to you on the condition that you agree to the [supplemental terms of use](https://azure.microsoft.com/support/legal/preview-supplemental-terms/). Some aspects of this feature might change before general availability (GA).
2423
24+
In this quickstart, you run a code sample that demonstrates how a JavaScript single-page application (SPA) can sign in users of personal accounts, work accounts, and school accounts by using the authorization code flow. The code sample also demonstrates obtaining an access token to call a web API, in this case the Microsoft Graph API. See [How the sample works](#how-the-sample-works) for an illustration.
2525

26-
This quickstart uses MSAL.js 2.0 with the Authorization Code flow. To use MSAL.js 1.0 with the implicit flow, view [this quickstart](https://docs.microsoft.com/azure/active-directory/develop/quickstart-v2-javascript).
27-
28-
In this quickstart, you use a code sample to learn how a JavaScript single-page application (SPA) can sign in users of personal accounts, work accounts, and school accounts. A JavaScript SPA can also get an access token to call the Microsoft Graph API or any web API. See [How the sample works](#how-the-sample-works) for an illustration.
26+
This quickstart uses MSAL.js 2.0 with the authorization code flow. For a similar quickstart that uses MSAL.js 1.0 with the implicit flow, see [Quickstart: Sign in users in JavaScript single-page apps](https://docs.microsoft.com/azure/active-directory/develop/quickstart-v2-javascript).
2927

3028
## Prerequisites
3129

3230
* Azure subscription - [Create an Azure subscription for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F)
3331
* [Node.js](https://nodejs.org/en/download/)
34-
* [Visual Studio Code](https://code.visualstudio.com/download) (to edit project files)
35-
32+
* [Visual Studio Code](https://code.visualstudio.com/download) or another code editor
3633

3734
> [!div renderon="docs"]
3835
> ## Register and download your quickstart application
@@ -53,16 +50,15 @@ In this quickstart, you use a code sample to learn how a JavaScript single-page
5350
> #### Step 1: Register your application
5451
>
5552
> 1. Sign in to the [Azure portal](https://portal.azure.com).
56-
>
5753
> 1. If your account gives you access to more than one tenant, select your account at the top right, and then set your portal session to the Azure AD tenant you want to use.
5854
> 1. Select [App registrations](https://go.microsoft.com/fwlink/?linkid=2083908).
5955
> 1. Select **New registration**.
6056
> 1. When the **Register an application** page appears, enter a name for your application.
6157
> 1. Under **Supported account types**, select **Accounts in any organizational directory and personal Microsoft accounts**.
6258
> 1. Select **Register**. On the app **Overview** page, note the **Application (client) ID** value for later use.
6359
> 1. In the left pane of the registered application, select **Authentication**.
64-
> 1. Under **Platform Configurations**, select **Add a platform**. A panel opens on the left. There, select the **Single-Page Applications** region.
65-
> 1. Still on the left, set the **Redirect URI** value to `http://localhost:3000/`.
60+
> 1. Under **Platform configurations**, select **Add a platform**. In the pane that opens select **Single-page application**.
61+
> 1. Set the **Redirect URI** value to `http://localhost:3000/`.
6662
> 1. Select **Configure**.
6763
6864
> [!div class="sxs-lookup" renderon="portal"]
@@ -82,45 +78,52 @@ In this quickstart, you use a code sample to learn how a JavaScript single-page
8278
> [!div renderon="portal" class="sxs-lookup"]
8379
> Run the project with a web server by using Node.js
8480
85-
> [!div renderon="portal" id="autoupdate" class="nextstepaction" class="sxs-lookup"]
81+
> [!div renderon="portal" class="sxs-lookup" id="autoupdate" class="nextstepaction"]
8682
> [Download the code sample](https://github.com/Azure-Samples/ms-identity-javascript-v2/archive/master.zip)
8783
8884
> [!div renderon="docs"]
8985
> #### Step 3: Configure your JavaScript app
9086
>
91-
> In the *app* folder, edit *authConfig.js*, and set the `clientID`, `authority` and `redirectUri` values under `msalConfig`.
87+
> In the *app* folder, open the *authConfig.js* file and update the `clientID`, `authority`, and `redirectUri` values in the `msalConfig` object.
9288
>
9389
> ```javascript
94-
>
95-
> // Config object to be passed to Msal on creation
96-
> const msalConfig = {
97-
> auth: {
98-
> clientId: "Enter_the_Application_Id_Here",
99-
> authority: "Enter_the_Cloud_Instance_Id_HereEnter_the_Tenant_Info_Here",
100-
> redirectUri: "Enter_the_Redirect_Uri_Here",
101-
> },
102-
> cache: {
103-
> cacheLocation: "sessionStorage", // This configures where your cache will be stored
104-
> storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
105-
> }
106-
> };
107-
>
108-
>```
90+
> // Config object to be passed to Msal on creation
91+
> const msalConfig = {
92+
> auth: {
93+
> clientId: "Enter_the_Application_Id_Here",
94+
> authority: "Enter_the_Cloud_Instance_Id_HereEnter_the_Tenant_Info_Here",
95+
> redirectUri: "Enter_the_Redirect_Uri_Here",
96+
> },
97+
> cache: {
98+
> cacheLocation: "sessionStorage", // This configures where your cache will be stored
99+
> storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
100+
> }
101+
> };
102+
> ```
109103
110104
> [!div renderon="portal" class="sxs-lookup"]
111105
> > [!NOTE]
112-
> > :::no-loc text="Enter_the_Supported_Account_Info_Here":::
106+
> > `Enter_the_Supported_Account_Info_Here`
113107
114108
> [!div renderon="docs"]
115109
>
116-
> Where:
117-
> - *\<Enter_the_Application_Id_Here>* is the **Application (client) ID** for the application you registered.
118-
> - *\<Enter_the_Cloud_Instance_Id_Here>* is the instance of the Azure cloud. For the main or global Azure cloud, simply enter *https://login.microsoftonline.com/*. For **national** clouds (for example, China), see [National clouds](https://docs.microsoft.com/azure/active-directory/develop/authentication-national-cloud).
119-
> - *\<Enter_the_Tenant_info_here>* is set to one of the following options:
120-
> - If your application supports *accounts in this organizational directory*, replace this value with the **Tenant ID** or **Tenant name** (for example, *contoso.microsoft.com*).
121-
> - If your application supports *accounts in any organizational directory*, replace this value with **organizations**.
122-
> - If your application supports *accounts in any organizational directory and personal Microsoft accounts*, replace this value with **common**. To restrict support to *personal Microsoft accounts only*, replace this value with **consumers**.
123-
> - *\<Enter_the_Redirect_Uri_Here>* is `http://localhost:3000`
110+
> Modify the values in the `msalConfig` section as described here:
111+
>
112+
> - `Enter_the_Application_Id_Here` is the **Application (client) ID** for the application you registered.
113+
> - `Enter_the_Cloud_Instance_Id_Here` is the instance of the Azure cloud. For the main or global Azure cloud, enter `https://login.microsoftonline.com/`. For **national** clouds (for example, China), see [National clouds](authentication-national-cloud.md).
114+
> - `Enter_the_Tenant_info_here` is set to one of the following:
115+
> - If your application supports *accounts in this organizational directory*, replace this value with the **Tenant ID** or **Tenant name**. For example, `contoso.microsoft.com`.
116+
> - If your application supports *accounts in any organizational directory*, replace this value with `organizations`.
117+
> - If your application supports *accounts in any organizational directory and personal Microsoft accounts*, replace this value with `common`. **For this quickstart**, use `common`.
118+
> - To restrict support to *personal Microsoft accounts only*, replace this value with `consumers`.
119+
> - `Enter_the_Redirect_Uri_Here` is `http://localhost:3000/`.
120+
>
121+
> The `authority` value in your *authConfig.js* should be similar to the following if you're using the main (global) Azure cloud:
122+
>
123+
> ```javascript
124+
> authority: "https://login.microsoftonline.com/common",
125+
> ```
126+
>
124127
> > [!TIP]
125128
> > To find the values of **Application (client) ID**, **Directory (tenant) ID**, and **Supported account types**, go to the app registration's **Overview** page in the Azure portal.
126129
>
@@ -130,7 +133,8 @@ In this quickstart, you use a code sample to learn how a JavaScript single-page
130133
131134
> [!div renderon="docs"]
132135
>
133-
> Then, still in the same folder, edit *graphConfig.js* file to set the `graphMeEndpoint` and `graphMailEndpoint` for the `apiConfig` object.
136+
> Then, still in the same folder, edit the *graphConfig.js* file and update the `graphMeEndpoint` and `graphMailEndpoint` values in the `apiConfig` object.
137+
>
134138
> ```javascript
135139
> // Add here the endpoints for MS Graph API services you would like to use.
136140
> const graphConfig = {
@@ -144,31 +148,37 @@ In this quickstart, you use a code sample to learn how a JavaScript single-page
144148
> };
145149
> ```
146150
>
147-
148151
> [!div renderon="docs"]
149152
>
150-
> *\<Enter_the_Graph_Endpoint_Here>* is the endpoint that API calls will be made against. For the main or global Microsoft Graph API service, enter `https://graph.microsoft.com`. For more information, see [National cloud deployment](https://docs.microsoft.com/graph/deployments).
153+
> `Enter_the_Graph_Endpoint_Here` is the endpoint that API calls will be made against. For the main (global) Microsoft Graph API service, enter `https://graph.microsoft.com/` (include the trailing forward-slash). For more information about Microsoft Graph on national clouds, see [National cloud deployment](https://docs.microsoft.com/graph/deployments).
154+
>
155+
> The `graphMeEndpoint` and `graphMailEndpoint` values in the *graphConfig.js* file should be similar to the following if you're using the main (global) Microsoft Graph API service:
156+
>
157+
> ```javascript
158+
> graphMeEndpoint: "https://graph.microsoft.com/v1.0/me",
159+
> graphMailEndpoint: "https://graph.microsoft.com/v1.0/me/messages"
160+
> ```
151161
>
152162
> #### Step 4: Run the project
153163
154-
Run the project with a web server by using [Node.js](https://nodejs.org/en/download/):
164+
Run the project with a web server by using Node.js:
155165
156166
1. To start the server, run the following commands from within the project directory:
157-
```bash
167+
```console
158168
npm install
159169
npm start
160170
```
161171
1. Browse to `http://localhost:3000/`.
162172
163-
1. Select **Sign In** to start the sign-in process and then call Microsoft Graph API.
173+
1. Select **Sign In** to start the sign-in process and then call the Microsoft Graph API.
164174
165175
The first time you sign in, you're prompted to provide your consent to allow the application to access your profile and sign you in. After you're signed in successfully, your user profile information should be displayed on the page.
166176
167177
## More information
168178
169179
### How the sample works
170180
171-
![How the sample JavaScript SPA works: 1. The SPA initiates sign in. 2. The SPA acquires an ID token from the Microsoft identity platform. 3. The SPA calls acquire token. 4. The Microsoft identity platform returns an Access token to the SPA. 5. The SPA makes and HTTP GET request with the Access Token to the Microsoft Graph API. 6. The Graph API returns an HTTP response to the SPA.](media/quickstart-v2-javascript/javascriptspa-intro.svg)
181+
:::image type="content" source="media/quickstart-v2-javascript-auth-code/diagram-01-auth-code-flow.png" alt-text="Diagram showing the authorization code flow for a single-page application":::
172182
173183
### msal.js
174184
@@ -178,20 +188,16 @@ The MSAL.js library signs in users and requests the tokens that are used to acce
178188
<script type="text/javascript" src="https://alcdn.msauth.net/browser/2.0.0-beta.0/js/msal-browser.js" integrity=
179189
"sha384-r7Qxfs6PYHyfoBR6zG62DGzptfLBxnREThAlcJyEfzJ4dq5rqExc1Xj3TPFE/9TH" crossorigin="anonymous"></script>
180190
```
181-
> [!TIP]
182-
> You can replace the preceding version with the latest released version under [MSAL.js releases](https://github.com/AzureAD/microsoft-authentication-library-for-js/releases).
183191
184-
Alternatively, if you have Node.js installed, you can download the latest version by using the Node.js Package Manager (npm):
192+
If you have Node.js installed, you can download the latest version by using the Node.js Package Manager (npm):
185193
186-
```batch
194+
```console
187195
npm install @azure/msal-browser
188196
```
189197
190198
## Next steps
191199
192-
The [MSAL.js GitHub repo](https://github.com/AzureAD/microsoft-authentication-library-for-js) contains additional library documentation, a FAQ, and provides issue support.
193-
194-
For a more detailed step-by-step guide on building the application for this quickstart, see:
200+
For a more detailed step-by-step guide on building the application used in this quickstart, see the following tutorial:
195201
196202
> [!div class="nextstepaction"]
197-
> [Tutorial to sign in and call MS Graph](https://docs.microsoft.com/azure/active-directory/develop/tutorial-v2-javascript-auth-code)
203+
> [Tutorial to sign in and call MS Graph >](https://docs.microsoft.com/azure/active-directory/develop/tutorial-v2-javascript-auth-code)

0 commit comments

Comments
 (0)