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
# Sign in users in a sample Node.js & Express web app by using Microsoft Entra External ID for customers
26
+
# Sign in users in a sample Node.js (Express.js) web app by using Microsoft Entra External ID for customers
27
27
28
28
*[Overview](#overview)
29
29
*[Usage](#usage)
@@ -67,14 +67,14 @@ This sample demonstrates how to sign users into a sample Node.js & Express web a
67
67
* Microsoft Entra ID for customers tenant. If you don't already have one, [sign up for a free trial](https://aka.ms/ciam-free-trial).
68
68
* If you'd like to use Azure services, such as hosting your app in Azure App Service, [VS Code Azure Tools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-node-azure-pack) extension is recommended for interacting with Azure through VS Code Interface.
69
69
70
-
## Register web application in your tenant
71
-
72
-
To enable your application to sign in users with Microsoft Entra ID for customers, Microsoft Entra ID for customers must be made aware of the application you create. The app registration establishes a trust relationship between the app and Microsoft Entra ID for customers. When you register an application, Microsoft Entra ID for customers generates a unique identifier known as an **Application (client) ID**, a value used to identify your app when creating authentication requests.
70
+
## Register the web application in your tenant
73
71
74
72
You can register an app in your tenant automatically by using Microsoft Graph PowerShell or Manually register your apps in Microsoft Entra Admin center.
75
73
76
74
When you use Microsoft Graph PowerShell, you automatically register the applications and related objects app secrets, then modifies your project config files, so you run the app without any further action.
77
75
76
+
To register your app manually in the Microsoft Entra admin center use the steps in [Register the web app](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#register-the-web-app).
77
+
78
78
<details>
79
79
<summary>Expand this section if you want to use this automation:</summary>
80
80
@@ -95,154 +95,37 @@ When you use Microsoft Graph PowerShell, you automatically register the applicat
95
95
96
96
</details>
97
97
98
-
To manually register your apps in Microsoft Entra Admin center, follow these steps:
99
-
100
-
1. Sign in to the [Microsoft Entra admin center](https://entra.microsoft.com) as at least an [Application Developer](https://learn.microsoft.com/entra/identity/role-based-access-control/permissions-reference#application-developer).
101
-
1. If you have access to multiple tenants, use the **Settings** icon () in the top menu to switch to your customer tenant from the **Directories + subscriptions** menu.
102
-
1. Browse to **Identity** >**Applications** > **App registrations**.
103
-
1. Select **+ New registration**.
104
-
1. In the **Register an application** page that appears;
105
-
106
-
1. Enter a meaningful application **Name** that is displayed to users of the app, for example *ciam-client-app*.
107
-
1. Under **Supported account types**, select **Accounts in this organizational directory only**.
108
-
109
-
1. Select **Register**.
110
-
1. The application's **Overview** pane displays upon successful registration. Record the **Application (client) ID** to be used in your application source code.
111
-
112
-
To specify your app type to your app registration, follow these steps:
113
-
114
-
1. Under **Manage**, select **Authentication**.
115
-
1. On the **Platform configurations** page, select **Add a platform**, and then select **Web** option.
116
-
1. For the **Redirect URIs** enter `http://localhost:3000/auth/redirect`.
117
-
1. Select **Configure** to save your changes.
118
-
119
98
## Add app client secret
120
99
121
-
Create a client secret for the registered application. The application uses the client secret to prove its identity when it requests for tokens:
122
-
123
-
1. From the **App registrations** page, select the application that you created (such as *ciam-client-app*) to open its **Overview** page.
124
-
1. Under **Manage**, select **Certificates & secrets**.
125
-
1. Select **New client secret**.
126
-
1. In the **Description** box, enter a description for the client secret (for example, *ciam app client secret*).
127
-
1. Under **Expires**, select a duration for which the secret is valid (per your organizations security rules), and then select **Add**.
128
-
1. Record the secret's **Value**. You'll use this value for configuration in a later step.
129
-
130
-
> :information_source: The secret value won't be displayed again, and is not retrievable by any means, after you navigate away from the **Certificates and secrets** page, so make sure you record it. For enhanced security, [Use client certificate for authentication in your Node.js web app instead of client secrets](https://learn.microsoft.com/entra/external-id/customers/how-to-web-app-node-use-certificate).
100
+
To create a client secret for the registered application, use the steps in [Add app client secret](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#add-app-client-secret)
131
101
132
102
## Grant API permissions
133
103
134
-
1. From the **App registrations** page, select the application that you created (such as *ciam-client-app*) to open its **Overview** page.
135
-
1. Under **Manage**, select **API permissions**.
136
-
1. Under **Configured permissions**, select **Add a permission**.
137
-
1. Select **Microsoft APIs** tab.
138
-
1. Under **Commonly used Microsoft APIs** section, select **Microsoft Graph**.
139
-
1. Select **Delegated permissions** option.
140
-
1. Under **Select permissions** section, search for and select both **openid** and **offline_access** permissions.
141
-
1. Select the **Add permissions** button.
142
-
1. At this point, you've assigned the permissions correctly. However, since the tenant is a customer's tenant, the consumer users themselves can't consent to these permissions. You as the admin must consent to these permissions on behalf of all the users in the tenant:
143
-
144
-
1. Select **Grant admin consent for \<your tenant name\>**, then select **Yes**.
145
-
1. Select **Refresh**, then verify that **Granted for \<your tenant name\>** appears under **Status** for both scopes.
104
+
To grant delegated permissions, use the steps in [Grant API permissions](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#grant-api-permissions).
146
105
147
106
## Create user flow
148
107
149
-
Follow these steps to create a user flow a customer can use to sign in or sign up for an application.
150
-
151
-
1. Sign in to the [Microsoft Entra admin center](https://entra.microsoft.com) as at least an [External ID User Flow Administrator](https://learn.microsoft.com/entra/identity/role-based-access-control/permissions-reference#external-id-user-flow-administrator).
152
-
1. If you have access to multiple tenants, use the **Settings** icon () in the top menu to switch to your customer tenant from the **Directories + subscriptions** menu.
153
-
1. Browse to **Identity** > **External Identities** > **User flows**.
154
-
1. Select **+ New user flow**.
155
-
1. On the **Create** page:
156
-
157
-
1. Enter a **Name** for the user flow, such as *SignInSignUpSample*.
158
-
1. In the **Identity providers** list, select **Email Accounts**. This identity provider allows users to sign-in or sign-up using their email address.
159
-
160
-
> :exclamation: NOTE: Additional identity providers will be listed here only after you set up federation with them. For example, if you set up federation with [Google](https://learn.microsoft.com/entra/external-id/customers/how-to-google-federation-customers) or [Facebook](https://learn.microsoft.com/entra/external-id/customers/how-to-facebook-federation-customers), you'll be able to select those additional identity providers here.
161
-
162
-
1. Under **Email accounts**, you can select one of the two options. For this tutorial, select **Email with password**.
163
-
164
-
- **Email with password**: Allows new users to sign up and sign in using an email address as the sign-in name and a password as their first factor credential.
165
-
- **Email one-time-passcode**: Allows new users to sign up and sign in using an email address as the sign-in name and email one-time passcode as their first factor credential.
166
-
167
-
> :exclamation: NOTE: Email one-time passcode must be enabled at the tenant level (**All Identity Providers** > **Email One-time-passcode**) for this option to be available at the user flow level.
168
-
169
-
1. Under **User attributes**, choose the attributes you want to collect from the user upon sign-up. By selecting **Show more**, you can choose attributes and claims for **Country/Region**, **Display Name**, and **Postal Code**. Select **OK**. (Users are only prompted for attributes when they sign up for the first time.)
170
-
171
-
1. Select **Create**. The new user flow appears in the **User flows** list. If necessary, refresh the page.
172
-
173
-
To enable self-service password reset, use the steps in [Enable self-service password reset](https://learn.microsoft.com/entra/external-id/customers/how-to-enable-password-reset-customers) article.
108
+
To create a user flow a customer can use to sign in or sign up for an application, use the steps in [Create a user flow](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#create-a-user-flow)
174
109
175
110
## Associate the web application with the user flow
176
111
177
-
Although many applications can be associated with your user flow, a single application can only be associated with one user flow. A user flow allows configuration of the user experience for specific applications. For example, you can configure a user flow that requires users to sign-in or sign-up with a phone number or email address.
178
-
179
-
1. On the sidebar menu, select **Identity**.
180
-
1. Select **External Identities**, then **User flows**.
181
-
1. In the **User flows** page, select the **User flow name** you created earlier, for example, *SignInSignUpSample*.
182
-
1. Under **Use**, select **Applications**.
183
-
1. Select **Add application**.
184
-
<!--[Screenshot the shows how to associate an application to a user flow.](media/20-create-user-flow-add-application.png)-->
185
-
1. Select the application from the list such as *ciam-client-app* or use the search box to find the application, and then select it.
186
-
187
-
1. Choose **Select**.
112
+
To associate the web application with the user flow, use the steps in [Associate the web application with the user flow](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#associate-the-web-application-with-the-user-flow).
188
113
189
114
## Clone or download sample web application
190
115
191
-
To get the web app sample code, [Download the .zip file](https://github.com/Azure-Samples/ms-identity-ciam-javascript-tutorial/archive/refs/heads/main.zip) or clone the sample web application from GitHub by running the following command:
If you choose to download the *.zip* file, extract the sample app file to a folder where the total length of the path is 260 or fewer characters.
116
+
To get the web app sample code, use the steps in [Clone or download sample web application](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#clone-or-download-sample-web-application).
198
117
199
118
## Install project dependencies
200
119
201
-
1. Open a console window, and change to the directory that contains the Node.js sample app:
202
-
203
-
```console
204
-
cd 1-Authentication\5-sign-in-express\App
205
-
```
206
-
207
-
1. Run the following commands to install app dependencies:
208
-
209
-
```console
210
-
npm install
211
-
```
120
+
To install app dependencies, use the steps in [Install project dependencies](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#install-project-dependencies).
212
121
213
122
## Configure the sample web app to use your app registration
214
123
215
-
1. In your code editor, open *App\authConfig.js* file.
216
-
217
-
1. Find the placeholder:
218
-
219
-
1.`Enter_the_Application_Id_Here` and replace it with the Application (client) ID of the app you registered earlier.
220
-
221
-
1.`Enter_the_Tenant_Subdomain_Here` and replace it with the Directory (tenant) subdomain. For example, if your tenant primary domain is `contoso.onmicrosoft.com`, use `contoso`. If you don't have your tenant name, learn how to [read your tenant details](https://learn.microsoft.com/entra/external-id/customers/how-to-create-customer-tenant-portal#get-the-customer-tenant-details).
222
-
223
-
1.`Enter_the_Client_Secret_Here` and replace it with the app secret value you copied earlier.
124
+
Once you download the sample app, you need to update it so that it uses the settings of the web app that you registered. To do so, use the steps in [Configure the sample web app](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#configure-the-sample-web-app).
224
125
225
126
## Run and test sample web app
226
127
227
-
You can now test the sample Node.js web app. You need to start the Node.js server and access it through your browser at `http://localhost:3000`.
228
-
229
-
1. In your terminal, run the following command:
230
-
231
-
```console
232
-
npm start
233
-
```
234
-
235
-
1. Open your browser, then go to http://localhost:3000.
236
-
237
-
1. After the page completes loading, select **Sign in** link. You're prompted to sign in.
238
-
239
-
1. On the sign-in page, type your **Email address**, select **Next**, type your **Password**, then select **Sign in**. If you don't have an account, select **No account? Create one** link, which starts the sign-up flow.
240
-
241
-
1. If you choose the sign-up option, after filling in your email, one-time passcode, new password and more account details, you complete the whole sign-up flow. You see a page similar to the following screenshot. You see a similar page if you choose the sign-in option.
242
-
243
-

244
-
245
-
1. Select **Sign out** to sign the user out of the web app or select **View ID token claims** to view ID token claims returned by Microsoft Entra.
128
+
You can now test the sample Node.js web app. You need to start the Node.js server and access it through your browser at `http://localhost:3000`. To do so, use the steps in [Run and test sample web app](https://learn.microsoft.com/entra/external-id/customers/sample-web-app-node-sign-in#run-and-test-sample-web-app).
246
129
247
130
> :information_source: If the sample didn't work for you as expected, reach out to us using the [GitHub Issues](../../../../issues) page.
248
131
@@ -359,9 +242,9 @@ When you want to sign the user out of the application, it isn't enough to end th
359
242
360
243
There is one web app in this sample. To deploy it to **Azure App Services**, you'll need to:
361
244
362
-
-Create an **Azure App Service**
363
-
-Publish the projects to the **App Services**, and
364
-
-Update its client(s) to call the website instead of the local environment.
245
+
*Create an **Azure App Service**
246
+
*Publish the projects to the **App Services**, and
247
+
*Update its client(s) to call the website instead of the local environment.
365
248
366
249
#### Deploy your files of your web app
367
250
@@ -400,4 +283,4 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
400
283
*[Single sign-on with MSAL.js](https://learn.microsoft.com/entra/identity-platform/msal-js-sso)
401
284
*[Handle MSAL.js exceptions and errors](https://learn.microsoft.com/entra/msal/dotnet/advanced/exceptions/msal-error-handling?tabs=javascript)
402
285
*[Logging in MSAL.js applications](https://learn.microsoft.com/entra/msal/dotnet/advanced/exceptions/msal-logging?tabs=javascript)
403
-
*[Pass custom state in authentication requests using MSAL.js](https://learn.microsoft.com/entra/identity-platform/msal-js-pass-custom-state-authentication-request)
286
+
*[Pass custom state in authentication requests using MSAL.js](https://learn.microsoft.com/entra/identity-platform/msal-js-pass-custom-state-authentication-request).
0 commit comments