Search code examples
angularjsazure-active-directoryazure-ad-msalazure-ad-graph-apimsal-angular

Not getting token with acquireTokensilent but getting tokens with acquireTokenpopup Msal-browser


I am updating MSAL version 1.x to MSAL-browser in angular.So I am trying to migrate from version 1.x to 2.X.I was able to replace code successfully and it is working fine. but I am getting issue with acquireTokenSilent but its working fine by using acquireTokenPopup. I am following this link: https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/v1-migration.md. This is latest library published 11 days ago only and not much inputs i am able to get from internet other then basic documentation. The thing is when i am using acquireTokenSilent its giving error as: getTokenerror BrowserAuthError: no_account_error: No account object provided to acquireTokenSilent and no active account has been set but if i am using acquireTokenPopup at the same place, my application is working. Sample:

public Tokenfunction(): void {
    this.MSALobject.acquireTokenSilent(request).then(response => {
       this.tokenSubject.next(response.idToken);
    })
    .catch((error: msal.AuthError) => {
      if (error) {
        return this.login();
      }
       somefunction();
      
    });
  }

but instead of acquireTokenSilent if i use acquireTokenPopup, its working fine i am not getting what is the issue.


Solution

  • Summarize the comments into the answer in order to archive this question:

    Giving account object in the request can resolve this issue.

    Earlier was giving like this: request= { scopes: ['profile'] }; Add one more line with user account as account : msalobj.getAllAccounts()[0].

    I think you don't need to handle it by yourself because acquireTokenSilent can handle the refresh token.