0

I am facing a problem when i am trying to get the accounts that have already logged in in the browser.

In my index.tsx file i am doing this

import { AccountInfo, PublicClientApplication } from "@azure/msal-browser"

const msalInstance: PublicClientApplication = new PublicClientApplication(
  msalConfig
);

const accounts: AccountInfo[] = msalInstance.getAllAccounts()
console.log(accounts); //[]

i am already logged in with an account in another application opened in another tab, so i would like to get the information of that account and use them in my login.tsx using:

import { useMsal } from "@azure/msal-react";

const { instance, accounts, inProgress } = useMsal();

instance.acquireTokenSilent({
  scopes: [user.read],
  account: accounts[0]
})

i read that users information are stored in the cache somehow, so how can i get that accounts list?


Update

I found out that is possible to see if a user log-in/out from another browser tab using a callback ad check the EventType.ACCOUNT_ADDED or EventType.ACCOUNT_REMOVED so I tried this code

msalInstance.enableAccountStorageEvents();

msalInstance.addEventCallback((message: EventMessage) => {
  if (message.eventType === EventType.ACCOUNT_ADDED && message.payload) {
    const payload = message.payload as AccountInfo;
    msalInstance.setActiveAccount(payload);
    console.log("someone logged in another tab: ", payload);
  }

  if (message.eventType === EventType.ACCOUNT_REMOVED) {
    msalInstance.setActiveAccount(null);
    console.log("someone out in another tab");
  }
});

but I tried to log-in/out from another tab and nothing happens in the console. Any suggestions?

Riccardo
  • 3
  • 4

0 Answers0