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.

How to get auth code using msal

See original GitHub issue

Core Library

MSAL.js v2 (@azure/msal-browser)

Wrapper Library

MSAL React (@azure/msal-react)

Documentation Location

https://github.com/AzureAD/microsoft-authentication-library-for-js

Description

is it possible to request auth code (https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow) using this library for React? If so, is it possible for someone to give me a code sample? I’ve tried looking around but have had a really difficult time getting the auth code.

By using this Azure sample and following this tutorial, I’m able to get the accessToken but can’t figure out how to get the auth code (the response is returning null for code: null) to pass to our backend.

Currently, we’re using this code snippet to show a popup and get the accessToken in the response. Is it possible to configure the code snippet below to only get the auth code so we can pass that to our backend to use and exchange for tokens?

instance.acquireTokenPopup(loginRequest).then(response => {
            }).catch(e => {
            })

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
derisencommented, Jun 24, 2022

@mikeyyyzhao msal-browser/msal-react does not expose the authorization code to the developer, and the authorization code obtained for your frontend shouldn’t be passed to your backend. If you need to authenticate users to your backend, you should use msal-node instead. If you need to authenticate users on both frontend and backend, you might want to look into the hybrid spa auth code sample here.

0reactions
xsegritycommented, Aug 11, 2022

@derisen I have the same issue as @logan-jobzmall. I have an angular based spa app where I want the user to grant authorization for their personal Outlook Calendar so that my back end server can then add/update events on their behalf on an ongoing, non-interactive basis. This is easy in Google where I get an AccessCode on the client that I then send to the server to trade for an AccessToken/RefreshToken, then store the RefreshToken for later API usage without user interaction.

Is this even possible with MSAL/MS Graph?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authentication flow support in MSAL - Microsoft Learn
Requests an authorization code which redeemed for an access token. · Acquires a token by using application secret or password credentials.
Read more >
Acquiring tokens with authorization codes on web apps - GitHub
Getting tokens by authorization code in MSAL.NET. This principle is illustrated below the code performing the application initialization ...
Read more >
AAD/B2C Custom SignUp - Using Auth Code Flow - How can I ...
Now, we do get a code back from AAD/B2C, but how do we get MSAL to do the exchange for the tokens and...
Read more >
MSAL Python 1.20.0 documentation
You can ask authorization code for multiple resources, but when you redeem it, the token is for only one intended recipient, called audience....
Read more >
Microsoft Authentication Library for JavaScript (MSAL.js)
In MSAL, you can get access tokens for the APIs your app needs to call using the acquireTokenSilent method which makes a silent...
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