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
# Customer intent: As an application developer, I want to know how to set up authentication in a web application built using Node.js and MSAL Node.
17
17
---
18
18
19
-
In this quickstart, you download and run a code sample that demonstrates how a Node.js web app can sign in users by using the authorization code flow. The code sample also demonstrates how to get an access token to call Microsoft Graph API.
19
+
In this quickstart, you download and run a code sample that demonstrates how a Node.js web app can sign in users by using the authorization code flow. The code sample also demonstrates how to get an access token to call the Microsoft Graph API.
20
20
21
21
See [How the sample works](#how-the-sample-works) for an illustration.
22
22
@@ -37,8 +37,8 @@ This quickstart uses the Microsoft Authentication Library for Node.js (MSAL Node
37
37
1. If you have access to multiple tenants, use the **Directories + subscriptions** filter :::image type="icon" source="../../media/common/portal-directory-subscription-filter.png" border="false"::: in the top menu to switch to the tenant in which you want to register the application.
38
38
1. Under **Manage**, select **App registrations** > **New registration**.
39
39
1. Enter a **Name** for your application. Users of your app might see this name, and you can change it later.
40
-
1. Under **Supported account types**, select **Accounts in any organizational directory and personal Microsoft accounts**.
41
-
1. Set the **Redirect URI** value to `http://localhost:3000/redirect`.
40
+
1. Under **Supported account types**, select **Accounts in this organizational directory only**.
41
+
1. Set the **Redirect URI** value to `http://localhost:3000/auth/redirect`.
42
42
1. Select **Register**.
43
43
1. On the app **Overview** page, note the **Application (client) ID** value for later use.
44
44
1. Under **Manage**, select **Certificates & secrets** > **Client secrets** > **New client secret**. Leave the description blank and default expiration, and then select **Add**.
@@ -51,67 +51,57 @@ To run the project with a web server by using Node.js, [download the core projec
51
51
52
52
#### Step 3: Configure your Node app
53
53
54
-
Extract the project, open the *ms-identity-node-main* folder, and then open the *index.js* file.
55
-
56
-
Set the `clientID` value with the application (client) ID, and then set the `clientSecret` value with the client secret.
Extract the project, open the *ms-identity-node-main* folder, and then open the *.env* file under the *App* folder. Replace the values above as follows:
77
55
78
-
Modify the values in the `config` section:
56
+
| Variable | Description | Example(s) |
57
+
|-----------|--------------|------------|
58
+
|`Enter_the_Cloud_Instance_Id_Here`| The Azure cloud instance in which your application is registered |`https://login.microsoftonline.com/` (include the trailing forward-slash) |
59
+
|`Enter_the_Tenant_Info_here`| Tenant ID or Primary domain |`contoso.microsoft.com` or `cbe899ec-5f5c-4efe-b7a0-599505d3d54f`|
60
+
|`Enter_the_Application_Id_Here`| Client ID of the application you registered |`cbe899ec-5f5c-4efe-b7a0-599505d3d54f`|
61
+
|`Enter_the_Client_Secret_Here`| Client secret of the application you registered |`WxvhStRfDXoEiZQj1qCy`|
62
+
|`Enter_the_Graph_Endpoint_Here`| The Microsoft Graph API cloud instance that your app will call |`https://graph.microsoft.com/` (include the trailing forward-slash) |
63
+
|`Enter_the_Express_Session_Secret_Here`| A random string of characters used to sign the Express session cookie |`WxvhStRfDXoEiZQj1qCy`|
79
64
80
-
-`Enter_the_Application_Id_Here` is the application (client) ID for the application you registered.
65
+
Your file should look similar to below:
81
66
82
-
To find the application (client) ID, go to the app registration's **Overview** page in the Azure portal.
83
-
-`Enter_the_Client_Secret_Here` is the client secret for the application you registered.
67
+
```text
68
+
CLOUD_INSTANCE=https://login.microsoftonline.com/
69
+
TENANT_ID=cbe899ec-5f5c-4efe-b7a0-599505d3d54f
70
+
CLIENT_ID=fa29b4c9-7675-4b61-8a0a-bf7b2b4fda91
71
+
CLIENT_SECRET=WxvhStRfDXoEiZQj1qCy
84
72
85
-
To retrieve or generate a new client secret, under **Manage**, select **Certificates & secrets**.
73
+
REDIRECT_URI=http://localhost:3000/auth/redirect
74
+
POST_LOGOUT_REDIRECT_URI=http://localhost:3000
86
75
87
-
The default `authority` value represents the main (global) Azure cloud:
1. To start the server, run the following commands from within the project directory:
98
87
99
88
```console
89
+
cd App
100
90
npm install
101
91
npm start
102
92
```
103
93
104
94
1. Go to `http://localhost:3000/`.
105
95
106
-
1. Select **Sign In** to start the sign-in process.
96
+
1. Select **Sign in** to start the sign-in process.
107
97
108
-
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, you will see a log message in the command line.
98
+
The first time you sign in, you're prompted to provide your consent to allow the application to sign you in and access your profile. After you're signed in successfully, you'll be redirected back to the application home page.
109
99
110
100
## More information
111
101
112
102
### How the sample works
113
103
114
-
The sample hosts a web server on localhost, port 3000. When a web browser accesses this site, the sample immediately redirects the user to a Microsoft authentication page. Because of this, the sample does not contain any HTML or display elements. Authentication success displays the message "OK".
104
+
The sample hosts a web server on localhost, port 3000. When a web browser accesses this address, the app renders the home page. Once the user selects **Sign in**, the app redirects the browser to Azure AD sign-in screen, via the URL generated by the MSAL Node library. After user consents, the browser redirects the user back to the application home page, along with an ID and access token.
115
105
116
106
### MSAL Node
117
107
@@ -123,5 +113,6 @@ npm install @azure/msal-node
123
113
124
114
## Next steps
125
115
116
+
Learn more about the web app scenario that the Microsoft identity platform supports:
126
117
> [!div class="nextstepaction"]
127
-
> [Adding Auth to an existing web app - GitHub code sample >](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/auth-code)
118
+
> [Web app that signs in users scenario](../../scenario-web-app-sign-user-overview.md)
In the Azure portal, the reply URIs that you register on the Authentication page for your application need to match the redirectUri instances that the application defines (`http://localhost:3000/redirect`).
191
+
In the Azure portal, the reply URIs that you register on the Authentication page for your application need to match the redirectUri instances that the application defines (`http://localhost:3000/auth/redirect`).
208
192
209
193
> [!NOTE]
210
194
> This quickstart proposes to store the client secret in the configuration file for simplicity. In your production app, you'd want to use other ways to store your secret, such as a key vault or an environment variable.
@@ -350,12 +334,9 @@ For details about the authorization code flow that this method triggers, see the
350
334
351
335
# [Node.js](#tab/nodejs)
352
336
353
-
```javascript
354
-
const msal= require('@azure/msal-node');
337
+
Node sample the Express framework. MSALis initialized in*auth* route handler:
355
338
356
-
// Create msal application object
357
-
const cca= new msal.ConfidentialClientApplication(config);
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-web-app-sign-user-app-registration.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -96,8 +96,8 @@ By default, the sample uses:
96
96
97
97
1. When the **Register an application page** appears, enter your application's registration information:
98
98
1. Enter a **Name** for your application, for example `node-webapp`. Users of your app might see this name, and you can change it later.
99
-
1. Change **Supported account types** to **Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com)**.
100
-
1. In the **Redirect URI (optional)** section, select **Web** in the combo box and enter the following redirect URI: `http://localhost:3000/redirect`.
99
+
1. Change **Supported account types** to **Accounts in this organizational directory only**.
100
+
1. In the **Redirect URI (optional)** section, select **Web** in the combo box and enter the following redirect URI: `http://localhost:3000/auth/redirect`.
101
101
1. Select **Register** to create the application.
102
102
1. On the app's **Overview** page, find the **Application (client) ID** value and record it for later. You'll need it to configure the configuration file for this project.
103
103
1. Under **Manage**, select **Certificates & secrets**.
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-web-app-sign-user-sign-in.md
+16-45Lines changed: 16 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,7 @@ else
72
72
73
73
# [Java](#tab/java)
74
74
75
-
In our Java quickstart, the sign-in button is located in the [main/resources/templates/index.html](https://github.com/Azure-Samples/ms-identity-java-webapp/blob/master/msal-java-webapp-sample/src/main/resources/templates/index.html) file.
75
+
In the Java quickstart, the sign-in button is located in the [main/resources/templates/index.html](https://github.com/Azure-Samples/ms-identity-java-webapp/blob/master/msal-java-webapp-sample/src/main/resources/templates/index.html) file.
76
76
77
77
```html
78
78
<!DOCTYPE html>
@@ -94,13 +94,13 @@ In our Java quickstart, the sign-in button is located in the [main/resources/tem
94
94
95
95
# [Node.js](#tab/nodejs)
96
96
97
-
In the Node.js quickstart, there's no sign-in button. The code-behind automatically prompts the user for sign-in when it's reaching the root of the web app.
97
+
In the Node.js quickstart, the code for the sign-in button is located in *index.hbs* template file.
When the user selects the **Sign in** link, which triggers the `/auth/signin` route, the sign-in controller takes over to authenticate the user with Microsoft identity platform.
@@ -431,7 +400,9 @@ In Java, sign-out is handled by calling the Microsoft identity platform `logout`
431
400
432
401
# [Node.js](#tab/nodejs)
433
402
434
-
This sample application does not implement sign-out.
403
+
When the user selects the **Sign out** button, the app triggers the `/signout` route, which destroys the session and redirects the browser to Microsoft identity platform sign-out endpoint.
@@ -479,7 +450,7 @@ In the Java quickstart, the post-logout redirect URI just displays the index.htm
479
450
480
451
# [Node.js](#tab/nodejs)
481
452
482
-
This sample application does not implement sign-out.
453
+
In the Node quickstart, the post-logout redirect URI is used to redirect the browser back to sample home page after the user completes the logout process with the Microsoft identity platform.
483
454
484
455
# [Python](#tab/python)
485
456
@@ -494,4 +465,4 @@ If you want to learn more about sign-out, read the protocol documentation that's
494
465
## Next steps
495
466
496
467
Move on to the next article in this scenario,
497
-
[Move to production](scenario-web-app-sign-user-production.md).
468
+
[Move to production](scenario-web-app-sign-user-production.md).
0 commit comments