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/active-directory/develop/quickstart-v2-javascript.md
+69-61Lines changed: 69 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Sign in users in JavaScript single-page apps | Azure
3
3
titleSuffix: Microsoft identity platform
4
-
description: Learn how a Javascript app can an API that requires access tokens using the Microsoft identity platform.
4
+
description: Learn how a JavaScript app can call an API that requires access tokens using the Microsoft identity platform.
5
5
services: active-directory
6
6
author: navyasric
7
7
manager: CelesteDG
@@ -26,7 +26,7 @@ In this quickstart, you use a code sample to learn how a JavaScript single-page
26
26
27
27
* Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F)
28
28
*[Node.js](https://nodejs.org/en/download/).
29
-
*Either [Visual Studio Code](https://code.visualstudio.com/download) (to edit project files), or [Visual Studio 2019](https://visualstudio.microsoft.com/downloads/) (ro run the project as a Visual Studio Solution).
29
+
*[Visual Studio Code](https://code.visualstudio.com/download) (to edit project files)
30
30
31
31
> [!div renderon="docs"]
32
32
> ## Register and download your quickstart application
@@ -37,7 +37,9 @@ In this quickstart, you use a code sample to learn how a JavaScript single-page
37
37
> 1. Sign in to the [Azure portal](https://portal.azure.com) by using either a work or school account, or a personal Microsoft account.
38
38
> 1. If your account gives you access to more than one tenant, select the account at the top right, and then set your portal session to the Azure Active Directory (Azure AD) tenant you want to use.
39
39
> 1. Go to the new [Azure portal - App registrations](https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade/quickStartType/JavascriptSpaQuickstartPage/sourceType/docs) pane.
40
-
> 1. Enter a name for your application, and select **Register**.
40
+
> 1. Enter a name for your application.
41
+
> 1. Under **Supported account types**, select **Accounts in any organizational directory and personal Microsoft accounts**.
42
+
> 1. Select **Register**.
41
43
> 1. Follow the instructions to download and automatically configure your new application.
42
44
>
43
45
> ### Option 2 (Manual): Register and manually configure your application and code sample
@@ -51,15 +53,15 @@ In this quickstart, you use a code sample to learn how a JavaScript single-page
51
53
> 1. Select **New registration**.
52
54
> 1. When the **Register an application** page appears, enter a name for your application.
53
55
> 1. Under **Supported account types**, select **Accounts in any organizational directory and personal Microsoft accounts**.
54
-
> 1. Under the **Redirect URI** section, in the drop-down list, select the **Web** platform, and then set the value to `http://localhost:30662/`.
55
56
> 1. Select **Register**. On the app **Overview** page, note the **Application (client) ID** value for later use.
56
57
> 1. This quickstart requires the [Implicit grant flow](v2-oauth2-implicit-grant-flow.md) to be enabled. In the left pane of the registered application, select **Authentication**.
57
-
> 1. In the **Advanced settings** section, under **Implicit grant**, select the **ID tokens** and **Access tokens** check boxes. ID tokens and access tokens are required, because this app needs to sign in users and call an API.
58
-
> 1. At the top of the pane, select **Save**.
58
+
> 1. Under **Platform Configurations**, select **Add a platform**. A panel opens on the left. There, select the **Web Applications** region.
59
+
> 1. Still on the left, set the **Redirect URI** value to `http://localhost:3000/`. Then, select **Access Token** and **ID Token**.
60
+
> 1. Select **Configure**.
59
61
60
62
> [!div class="sxs-lookup" renderon="portal"]
61
63
> #### Step 1: Configure your application in the Azure portal
62
-
> For the code sample for this quickstart to work, you need to add a `redirectUri` as `http://localhost:30662/` and enable **Implicit grant**.
64
+
> To make the code sample in this quickstart work, you need to add a `redirectUri` as `http://localhost:3000/` and enable **Implicit grant**.
@@ -72,39 +74,40 @@ Select the option that's suitable to your development environment:
72
74
73
75
* To run the project with a web server by using Node.js, [download the core project files](https://github.com/Azure-Samples/active-directory-javascript-graphapi-v2/archive/quickstart.zip). To open the files, use an editor such as [Visual Studio Code](https://code.visualstudio.com/).
74
76
75
-
* (Optional) To run the project with the IIS server, [download the Visual Studio project](https://github.com/Azure-Samples/active-directory-javascript-graphapi-v2/archive/vsquickstart.zip). Extract the zip file to a local folder (for example, *C:\Azure-Samples*).
76
-
77
77
#### Step 3: Configure your JavaScript app
78
78
79
79
> [!div renderon="docs"]
80
-
> In the *JavaScriptSPA* folder, edit *index.html*, and set the `clientID` and `authority` values under `msalConfig`.
80
+
> In the *JavaScriptSPA* folder, edit *authConfig.js*, and set the `clientID` and `authority` values under `msalConfig`.
81
81
82
82
> [!div class="sxs-lookup" renderon="portal"]
83
-
> In the *JavaScriptSPA* folder, edit *index.html*, and replace `msalConfig` with the following code:
83
+
> In the *JavaScriptSPA* folder, edit *authConfig.js*, and replace `msalConfig` with the following code:
cacheLocation:"sessionStorage", // This configures where your cache will be stored
96
+
storeAuthStateInCookie:false, // Set this to "true" if you are having issues on IE11 or Edge
97
+
forceRefresh:false// Set this to "true" to skip a cached token and go to the server to get a new
95
98
}
96
-
};
99
+
};
97
100
98
101
```
99
102
> [!div renderon="portal"]
100
103
> > [!NOTE]
101
104
> > This quickstart supports Enter_the_Supported_Account_Info_Here.
102
105
103
-
104
106
> [!div renderon="docs"]
105
107
>
106
108
> Where:
107
-
> -*\<Enter_the_Application_Id_here>* is the **Application (client) ID** for the application you registered.
109
+
> -*\<Enter_the_Application_Id_Here>* is the **Application (client) ID** for the application you registered.
110
+
> -*\<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).
108
111
> -*\<Enter_the_Tenant_info_here>* is set to one of the following options:
109
112
> - If your application supports *accounts in this organizational directory*, replace this value with the **Tenant ID** or **Tenant name** (for example, *contoso.microsoft.com*).
110
113
> - If your application supports *accounts in any organizational directory*, replace this value with **organizations**.
@@ -116,20 +119,17 @@ var msalConfig = {
116
119
117
120
#### Step 4: Run the project
118
121
119
-
* If you're using [Node.js](https://nodejs.org/en/download/):
120
-
121
-
1. To start the server, run the following command from the project directory:
122
-
123
-
```batch
124
-
npm install
125
-
node server.js
126
-
```
122
+
If you're using [Node.js](https://nodejs.org/en/download/):
127
123
128
-
1. Open a web browser and go to `http://localhost:30662/`.
129
-
1. Select **Sign In** to start the sign-in, and then call Microsoft Graph API.
124
+
1. To start the server, run the following command from the project directory:
130
125
126
+
```batch
127
+
npm install
128
+
npm start
129
+
```
131
130
132
-
* If you're using [Visual Studio](https://visualstudio.microsoft.com/downloads/), select the project solution, and then select F5 to run the project.
131
+
1. Open a web browser and go to `http://localhost:3000/`.
132
+
1. Select **Sign In** to start the sign-in, and then call Microsoft Graph API.
133
133
134
134
After the browser loads the application, select **Sign In**. The first time that you sign in, you're prompted to provide your consent to allow the application to access your profile and to sign you in. After you're signed in successfully, your user profile information should be displayed on the page.
135
135
@@ -144,7 +144,7 @@ After the browser loads the application, select **Sign In**. The first time that
144
144
The MSAL library signs in users and requests the tokens that are used to access an API that's protected by Microsoft identity platform. The quickstart *index.html* file contains a reference to the library:
> 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).
@@ -161,26 +161,28 @@ npm install msal
161
161
The quickstart code also shows how to initialize the MSAL library:
> |`clientId`| The application ID of the application that's registered in the Azure portal.|
182
184
> |`authority`| (Optional) The authority URL that supports account types, as described previously in the configuration section. The default authority is `https://login.microsoftonline.com/common`. |
183
-
> |`redirectUri`| The application registration's configured reply/redirectUri. In this case, `http://localhost:30662/`. |
185
+
> |`redirectUri`| The application registration's configured reply/redirectUri. In this case, `http://localhost:3000/`. |
184
186
> |`cacheLocation`| (Optional) Sets the browser storage for the auth state. The default is sessionStorage. |
185
187
> |`storeAuthStateInCookie`| (Optional) The library that stores the authentication request state that's required for validation of the authentication flows in the browser cookies. This cookie is set for IE and Edge browsers to mitigate certain [known issues](https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/Known-issues-on-IE-and-Edge-Browser#issues). |
186
188
@@ -191,11 +193,13 @@ For more information about available configurable options, see [Initialize clien
191
193
The following code snippet shows how to sign in users:
192
194
193
195
```javascript
194
-
var requestObj = {
195
-
scopes: ["user.read"]
196
+
// Add scopes for the id token to be used at Microsoft identity platform endpoints.
@@ -218,21 +222,23 @@ MSAL uses three methods to acquire tokens: `acquireTokenRedirect`, `acquireToken
218
222
The `acquireTokenSilent` method handles token acquisitions and renewal without any user interaction. After the `loginRedirect` or `loginPopup` method is executed for the first time, `acquireTokenSilent` is the method commonly used to obtain tokens that are used to access protected resources for subsequent calls. Calls to request or renew tokens are made silently.
219
223
220
224
```javascript
221
-
var requestObj = {
222
-
scopes: ["user.read"]
225
+
// Add scopes for the access token to be used at Microsoft Graph API endpoints.
> |`scopes`| Contains scopes being requested to be returned in the access token for API. For example, `[ "user.read" ]` for Microsoft Graph or `[ "<Application ID URL>/scope" ]` for custom Web APIs (that is, `api://<Application ID>/access_as_user`).|
241
+
> |`scopes`| Contains scopes being requested to be returned in the access token for API. For example, `[ "mail.read" ]` for Microsoft Graph or `[ "<Application ID URL>/scope" ]` for custom Web APIs (that is, `api://<Application ID>/access_as_user`).|
236
242
237
243
#### Get a user token interactively
238
244
@@ -246,16 +252,18 @@ The usual recommended pattern for most applications is to call `acquireTokenSile
246
252
Calling the `acquireTokenPopup` results in a popup window for signing in. (Or `acquireTokenRedirect` results in redirecting users to the Microsoft identity platform endpoint.) In that window, users need to interact by confirming their credentials, giving the consent to the required resource, or completing the two-factor authentication.
247
253
248
254
```javascript
249
-
var requestObj = {
250
-
scopes: ["user.read"]
255
+
// Add here scopes for access token to be used at MS Graph API endpoints.
0 commit comments