TypeError: request.deviceCodeCallback is not a function when using MSAL Node

58 Views Asked by At

I'm encountering an issue with the MSAL Node library where I'm getting a TypeError regarding request.deviceCodeCallback not being a function. Here are the details:

  1. Relevant Code: I'm using the acquireTokenByDeviceCode method from the MSAL Node library to perform device code flow authentication. Here's the relevant code snippet:
const response = await publicApplication.acquireTokenByDeviceCode(authCodeUrlParameters, (deviceCodeResponse) => {
    console.log(deviceCodeResponse.message); // Here I'm logging the device code message
});

I've tried reviewing the documentation for MSAL Node and ensuring that my configuration is correct. I've also checked that the values for clientId, authority, and clientSecret are accurate. However, I'm still encountering this error.

This issue occurs specifically when using the device code flow authentication with MSAL Node. I'm developing a Node.js application and integrating with Microsoft Azure Active Directory for authentication purposes.

1

There are 1 best solutions below

0
Dasari Kamali On

I followed this MS DOC to get an access token from the Device code flow in JavaScript.

Code :

const msal = require('@azure/msal-node');

const msalConfig = {
    auth: {
        clientId: "<client_ID>",
        authority: "https://login.microsoftonline.com/<tenant_ID>",
    }
};

const pca = new msal.PublicClientApplication(msalConfig);

const deviceCodeRequest = {
    deviceCodeCallback: (response) => console.log(response.message),
    scopes: ["user.read"],
    timeout: 60, 
};

pca.acquireTokenByDeviceCode(deviceCodeRequest).then((response) => {
    console.log("Access token:", response.accessToken); 
}).catch((error) => {
    console.log(JSON.stringify(error));
});

I enabled the mobile and desktop flows in my Azure AD app as below.

enter image description here

Output :

It ran successfully as below:

enter image description here

Browser output:

I used the device login URL https://microsoft.com/devicelogin and the authentication code to log in from the browser as below.

enter image description here

I selected my account for login as below.

enter image description here

enter image description here

I successfully logged in using the Device code flow as below.

enter image description here

I successfully logged in from the device code flow and got the access token as shown below.

enter image description here