question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript - @azure/msal-node - accessToken type on AuthenticationResults

See original GitHub issue

Library

  • @azure/msal-node@1.x.x

Description

I am using the latest version of the @azure/msal-node module. I have a small typescript lambda that is creating an accessToken using the OAuth 2.0 Client Credentials Grant flow.

High level, I import the module:

import * as msal from '@azure/msal-node';

Setup my config:

  const config = {
    auth: {
      clientId: client_id,
      authority,
      clientSecret: client_secret,
    },
    system: {
      loggerOptions: {
        loggerCallback(loglevel: any, message: any, containsPii: any) {
          core.info(`${loglevel}:${message}:${containsPii} `);
        },
        piiLoggingEnabled: false,
        logLevel: msal.LogLevel.Verbose,
      },
    },
  };

Create the App & set scopes:

  const cca = new msal.ConfidentialClientApplication(config);

  const clientCredentialRequest = {
    scopes: ['api://xxxx/.default'],
  };

I then attempt to deconstruct the accessToken from the request by doing:

  try {
    const { accessToken } = await cca.acquireTokenByClientCredential(clientCredentialRequest);
    return accessToken;
  } catch (error) {
   log(error)
  }
}

However, I am getting the error:

Property 'accessToken' does not exist on type 'AuthenticationResult | null'.

On this line:

 const { accessToken } = await cca.acquireTokenByClientCredential(clientCredentialRequest);

I have tried finding the @azure/msal-node types library but I can’t find one.

I have got this working in Vanilla JS, but must admit I am new to TypeScript a little, so this is likely not a bug but on me, where about am I going wrong here, if you don’t mind me asking?

Source

  • Internal (Microsoft)
  • Customer request

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sameeragcommented, Apr 28, 2021

@NickLiffen Closing this issue. We will follow up on strict null checks enhancement, I just do not have a clear timeline at this point. Marked it internally.

1reaction
sameeragcommented, Apr 9, 2021

We added strict null checks in our code and hence had to support | null in cases where it applied. I do think that some of the changes can be refined and possibly remove this for certain return types, but this would need changes in a lot of code. We will take it up eventually and I will mark this as an enhancement in our backlog.

Hopefully we will get to it sooner than later 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

@azure/msal-node - npm
The web API validates the token, and calls Microsoft Graph on behalf of the user who authenticated in the web application.
Read more >
Tutorial: Sign in users in a Node.js & Express web app
The web app you build uses the Microsoft Authentication Library (MSAL) for Node. Follow the steps in this tutorial to: Register the application ......
Read more >
Single-page application: Acquire a token to call an API
The pattern for acquiring tokens for APIs with MSAL.js is to first attempt a silent token request by using the acquireTokenSilent method. When ......
Read more >
AuthorizationUrlRequest type alias - Microsoft Learn
Request object passed by user to retrieve a Code from the server (first leg of authorization code grant flow) scopes - Array of...
Read more >
@azure/msal-node package | Microsoft Learn
This class is to be used to acquire tokens for public client applications (desktop, mobile). Public client applications are not trusted to safely...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found