Edit

Quickstart: Sign in users in a sample Node.js CLI application

Applies to: Green circle with a white check mark symbol that indicates the following content applies to external tenants. External tenants (learn more)

In this quickstart, you use a sample Node Command Line Interface (CLI) application to sign in users in your external tenant. The sample application uses the Microsoft Authentication Library for Node (MSAL Node) to handle authentication.

Prerequisites

Enable public client flows

To identify your app as a public client, follow these steps:

  1. Under Manage, select Authentication.

  2. Under Advanced settings, for Allow public client flows, select Yes.

  3. Select Save to save your changes.

Clone or download the sample Node.js CLI application

To obtain the sample application, you can either clone it from GitHub or download it as a .zip file.

  • To clone the sample, open a command prompt and navigate to where you wish to create the project, and enter the following command:

    git clone https://github.com/Azure-Samples/ms-identity-ciam-javascript-tutorial.git
    
  • Download the .zip file. Extract it to a file path where the length of the name is fewer than 260 characters.

Configure the sample Node.js CLI application

To configure the client application (Node.js CLI app) to use your Microsoft Entra app registration details, open the project in your IDE and follow these steps:

  1. Open the App\authConfig.js file.

  2. Find the placeholder:

    • Enter_the_Application_Id_Here and replace the existing value with the application ID (clientId) of node-cli-app application copied from the Microsoft Entra admin center.
    • 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

Run and test the sample Node.js CLI application

You can now test the sample Node.js CLI application.

  1. In your terminal, run the following command:

    cd 1-Authentication\6-sign-in-node-cli-app\App
    npm start
    
  2. The browser opens up automatically and you should see a page similar to the following:

    Screenshot of the sign in page in a node CLI application.

  3. On the sign-in page, type your Email address. If you don't have an account, select No account? Create one, which starts the sign-up flow.

  4. 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. After completing the sign up flow and signing in, you see a page similar to the following screenshot:

    Screenshot showing a signed-in user in a node CLI application.

  5. Go back to the terminal and see your authentication information including the ID token claims.