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/tutorial-v2-javascript-auth-code.md
+41-27Lines changed: 41 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,25 +19,23 @@ ms.custom: aaddev
19
19
> [!IMPORTANT]
20
20
> 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).
21
21
22
-
This tutorial shows you how to create a JavaScript single-page application (SPA) that uses the Microsoft Authentication Library (MSAL) for JavaScript v2.0 (preview) to:
22
+
This tutorial shows you how to create a JavaScript single-page application (SPA) that uses the Microsoft Authentication Library (MSAL) for JavaScript v2.0 to:
23
23
24
24
> [!div class="checklist"]
25
25
> * Perform the OAuth 2.0 authorization code flow with PKCE
26
26
> * Sign in personal Microsoft accounts as well as work and school accounts
27
27
> * Acquire an access token
28
28
> * Call Microsoft Graph or your own API that requires access tokens obtained from the Microsoft identity platform endpoint
29
29
30
-
MSAL.js 2.0 (preview) improves on MSAL.js 1.0 by supporting the authorization code flow in the browser instead of using the implicit flow. MSAL.js 2.0 (preview) supports most features available in 1.0, but there are nuances to the authentication flow in both.
30
+
MSAL.js 2.0 improves on MSAL.js 1.0 by supporting the authorization code flow in the browser instead of the implicit grant flow. MSAL.js 2.0 does **NOT** support the implicit flow.
31
31
32
-
MSAL.js 2.0 (preview) does **NOT** support the implicit flow.
33
-
34
-
## How the sample app generated by this guide works
32
+
## How the tutorial app works
35
33
36
34

37
35
38
-
The sample application created by this guide enables a JavaScript SPA to query the Microsoft Graph API or a web API that accepts tokens from the Microsoft identity platform endpoint. In this scenario, after a user signs in, an access token is requested and added to HTTP requests through the authorization header. Token acquisition and renewal are handled by the Microsoft Authentication Library (MSAL).
36
+
The application you create in this tutorial enables a JavaScript SPA to query the Microsoft Graph API by acquiring security tokens from the the Microsoft identity platform endpoint. In this scenario, after a user signs in, an access token is requested and added to HTTP requests in the authorization header. Token acquisition and renewal are handled by the Microsoft Authentication Library for JavaScript (MSAL.js).
39
37
40
-
This guide uses the following library:
38
+
This tutorial uses the following library:
41
39
42
40
|||
43
41
|---|---|
@@ -61,7 +59,9 @@ To continue with the tutorial and build the application yourself, continue on to
61
59
62
60
## Create your project
63
61
64
-
Once you have [Node.js](https://nodejs.org/en/download/) installed, create a folder to host your application. Then, implement a small [Express](https://expressjs.com/) web server to serve your *index.html* file.
62
+
Once you have [Node.js](https://nodejs.org/en/download/) installed, create a folder to host your application, for example *msal-spa-tutorial*.
63
+
64
+
Next, implement a small [Express](https://expressjs.com/) web server to serve your *index.html* file.
65
65
66
66
1. First, change to your project directory in your terminal and then run the following `npm` commands:
67
67
```console
@@ -114,9 +114,20 @@ Once you have [Node.js](https://nodejs.org/en/download/) installed, create a fol
114
114
console.log(`Listening on port ${port}...`);
115
115
```
116
116
117
-
You now have a small webserver to serve your SPA. The folder structure you should have at the end of this tutorial is similar to the following:
117
+
You now have a small webserver to serve your SPA. After completing the rest of the tutorial, the file and folder structure of your project should look similar to the following:
118
118
119
-

119
+
```
120
+
msal-spa-tutorial/
121
+
├── app
122
+
│ ├── authConfig.js
123
+
│ ├── authPopup.js
124
+
│ ├── authRedirect.js
125
+
│ ├── graphConfig.js
126
+
│ ├── graph.js
127
+
│ ├── index.html
128
+
│ └── ui.js
129
+
└── server.js
130
+
```
120
131
121
132
## Create the SPA UI
122
133
@@ -130,7 +141,7 @@ You now have a small webserver to serve your SPA. The folder structure you shoul
@@ -547,44 +558,47 @@ In the sample application created in this tutorial, the `callMSGraph()` method i
547
558
548
559
You've completed creation of the application and are now ready to launch the Node.js web server and test the app's functionality.
549
560
550
-
1. Start the Node.js web server by running the following commands from within the application folder:
561
+
1. Start the Node.js web server by running the following command from within the root of your project folder:
551
562
552
563
```console
553
564
npm start
554
565
```
555
-
1. In your browser, enter`http://localhost:3000` or `http://localhost:<port>`, where `<port>` is the port that your web server is listening on. You should see the contents of your *index.html* file and the **Sign In** button.
566
+
1. In your browser, navigate to`http://localhost:3000` or `http://localhost:<port>`, where `<port>` is the port that your web server is listening on. You should see the contents of your *index.html* file and the **Sign In** button.
556
567
557
568
### Sign in to the application
558
569
559
570
After the browser loads your *index.html* file, select **Sign In**. You're prompted to sign in with the Microsoft identity platform endpoint:
:::image type="content" source="media/tutorial-v2-javascript-spa/spa-02-consent-dialog.png" alt-text="Content dialog displayed in web browser":::
568
579
569
-
### View application results
580
+
If you consent to the requested permissions, the web applications displays your user name, signifying a successful login:
570
581
571
-
After you sign in, your user profile information is returned in the Microsoft Graph API response that's displayed:
582
+
:::image type="content" source="media/tutorial-v2-javascript-spa/spa-03-signed-in.png" alt-text="Results of a successful sign-in in the web browser":::
572
583
573
-

584
+
### Call the Graph API
585
+
586
+
After you sign in, select **See Profile** to view the user profile information returned in the response from the call to the Microsoft Graph API:
587
+
588
+
:::image type="content" source="media/tutorial-v2-javascript-spa/spa-04-see-profile.png" alt-text="Profile information from Microsoft Graph displayed in the browser":::
574
589
575
590
### More information about scopes and delegated permissions
576
591
577
-
The Microsoft Graph API requires the *user.read* scope to read a user's profile. By default, this scope is automatically added in every application that's registered in the Azure portal. Other APIs for Microsoft Graph, as well as custom APIs for your back-end server, might require additional scopes. For example, the Microsoft Graph API requires the *Mail.Read* scope in order to list the user's mails.
592
+
The Microsoft Graph API requires the *user.read* scope to read a user's profile. By default, this scope is automatically added in every application that's registered in the Azure portal. Other APIs for Microsoft Graph, as well as custom APIs for your back-end server, might require additional scopes. For example, the Microsoft Graph API requires the *Mail.Read* scope in order to list the user's email.
578
593
579
-
> [!NOTE]
580
-
> The user might be prompted for additional consent as you add scopes.
594
+
As you add scopes, your users might be prompted to provide additional consent for the added scopes.
581
595
582
-
If a back-end API doesn't require a scope (not recommended), you can use *clientId* as the scope in the calls to acquire tokens.
596
+
If a back-end API doesn't require a scope, which isn't recommended, you can use `clientId` as the scope in the calls to acquire tokens.
583
597
584
598
[!INCLUDE [Help and support](../../../includes/active-directory-develop-help-support-include.md)]
585
599
586
600
## Next steps
587
601
588
602
To learn more about the auth code flow, as well as the differences between the implicit and authorization code flows, see [Microsoft identity platform and OAuth 2.0 authorization code flow](v2-oauth2-auth-code-flow.md).
589
603
590
-
If you'd like to dive deeper into JavaScript single-page application development on the Microsoft identity platform, the multi-part [Scenario: Single-page application](scenario-spa-overview.md) series of articles can help you get started.
604
+
If you'd like to dive deeper into JavaScript single-page application development on the Microsoft identity platform, the multi-part [Scenario: Single-page application](scenario-spa-overview.md) series of articles can help you get started.
0 commit comments