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.

msal-react acquiring tokens prior to API calls

See original GitHub issue

Library

  • @azure/msal-react@1.0.0-alpha.3

Description

I’m facing some issues with the way tokens have to be acquired in msal-react.

In short: I have all my async data fetch logic written in Redux actions (thunks to be more precise), my components only dispatch actions to trigger the data fetch and the data flows in once it’s retrieved from the server. Those async actions require a valid access token and as stated in the docs I should retrieve a fresh token for every API call, meaning that I’d have to bloat every component with access token logic and pass the the token to the action that fetches the data. I could put this logic in a hook and call the hook in every component, but the logic that acquires the token (IPublicClientApplication.acquireTokenSilent) is async, meaning that I’d have to return some kind of promise or state object from the hook and wait for it to complete, before I can fetch my data.

Ideally I’d like to have a valid token in my Redux store at all times and keep my components “clean” from token logic OR have the ability to retrieve a token outside of a React component (but this won’t be possible since it’s based on React Context) Any ideas on how to achieve a clean and scalable solution?

Thanks in advance!

Source

  • Internal (Microsoft)
  • Customer request

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Seddy24commented, Jan 31, 2021

@richardrobberse This worked perfectly, thank you for your help!

1reaction
richardrobbersecommented, Jan 31, 2021

@Seddy24 Here you go, my configuration is stored in Redux and the graphToken parameter just determines which scopes to request a token for.

import { Configuration, PublicClientApplication } from '@azure/msal-browser'
import { store } from 'store'

const getToken = async (graphToken: boolean = false): Promise<string> => {
  const appCfg = store.getState().app.configuration
  const cfg: Configuration = {
    auth: {
      clientId: appCfg.msalClientId,
      redirectUri: appCfg.msalRedirectUri,
    },
  }

  const pca = new PublicClientApplication(cfg)
  const accounts = pca.getAllAccounts()
  const account = accounts[0]

  const resp = await pca.acquireTokenSilent({
    scopes: graphToken ? appCfg.graphScopes : appCfg.apiScopes,
    account,
  })

  return resp.accessToken
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

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.
Read more >
How to acquire and use an access token from Azure AD in a ...
In the sample application that I used, I have a utility class called api.ts from where all API calls are made. By the...
Read more >
MSAL with React - acquire token as promise - Stack Overflow
I am currently developing an application in which I am using the React and MSAL libraries. According to the MS guide, in order...
Read more >
Calling Microsoft Graph using @azure/msal-react from PCF ...
Before we can call the MS Graph API, we must first acquire an access token. Underneath the hood, MSAL caches the tokens (i.e....
Read more >
Microsoft Authentication Library for JavaScript (MSAL.js)
The example below walks you through how to login a user and acquire a token to be used for Microsoft's Graph Api. Prerequisite....
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