Quickstart: Sign in users and get an access token in a Node.js web app using the auth code flow
Welcome! This probably isn't the page you were expecting. While we work on a fix, this link should take you to the right article:
We apologize for the inconvenience and appreciate your patience while we work to get this resolved.
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.
See How the sample works for an illustration.
This quickstart uses the Microsoft Authentication Library for Node.js (MSAL Node) with the authorization code flow.
- An Azure subscription. Create an Azure subscription for free.
- Visual Studio Code or another code editor
Step 1: Configure the application in Azure portal
For the code sample for this quickstart to work, you need to create a client secret and add the following reply URL:
Your application is configured with these > attributes.
Step 2: Download the project
Run the project with a web server by using Node.js.
Step 3: Your app is configured and ready to run
Run the project by using Node.js.
To start the server, run the following commands from within the project directory:
npm install npm start
Select Sign In to start the sign-in process.
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.
How the sample works
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".
The MSAL Node library signs in users and requests the tokens that are used to access an API that's protected by Microsoft identity platform. You can download the latest version by using the Node.js Package Manager (npm):
npm install @azure/msal-node