Authorization code is undefined i msal-react v3

111 views Asked by At

So , I have a requirement , where I've to send authorization code to the Backend inorder to get refresh and access token of the microsoft user. I'm using @azure/msal-react library which handles the popups and redirects , and get the access tokens and other credentials right. However , it returns the code to be undefined. Is there a way to fetch the authorization code using the same library ? . I'm including code for the following

const handleMicrosoftLogin = async () => {
    const loginRequest = {
      scopes: ['user.read', 'offline_access']
      // Add the necessary scopes
    }

    try {
      const res = await instance.acquireTokenPopup(loginRequest)
      console.log(res)
      // const user: MsJWTPayload = jwtDecode(res.idToken)

      // setCurrentMailbox({
      //   accessToken: res.accessToken,
      //   name: user?.name || '',
      //   email: user?.preferred_username || ''
      // })
      // setShowPasswordInput(true)
    } catch (error) {
      console.error('Login failed:', error)
    }
  }

here's my instance

export const msalConfig: Configuration = {
  auth: {
    clientId: import.meta.env.VITE_MICROSOFT_AUTH_CLIENT_ID,
    authority:
      'https://login.microsoftonline.com/679a768c-6cda-4071-ae37-5de31aeb7629',
    redirectUri: window.location.href
  }
}

export const pca = new PublicClientApplication(msalConfig)

and here's the response I'm getting

{
    "authority": "https://login.microsoftonline.com/common/",
    "uniqueId": "00000000-0000-0000-831a-ed4248869963",
    "tenantId": "9188040d-6c67-4c5b-b112-36a304b66dad",
    "scopes": [
        "User.Read",
        "openid",
        "profile"
    ],
    "code": undefined,
    "idToken": "abcdefXXXXX",
    "accessToken": "XXXXXX-----",
 ...rest of ms auth data
}
0

There are 0 answers