27 lines
592 B
TypeScript
27 lines
592 B
TypeScript
import {
|
|
isMsalConfigured,
|
|
MSAL_API_SCOPE,
|
|
MSAL_SILENT_REDIRECT_URI,
|
|
msalInstance,
|
|
} from './msal.config';
|
|
|
|
export async function acquireAccessToken(): Promise<string | null> {
|
|
if (!isMsalConfigured || !msalInstance) {
|
|
return null;
|
|
}
|
|
|
|
const accounts = msalInstance.getAllAccounts();
|
|
const account = msalInstance.getActiveAccount() ?? accounts[0];
|
|
if (!account) {
|
|
return null;
|
|
}
|
|
|
|
const response = await msalInstance.acquireTokenSilent({
|
|
account,
|
|
scopes: [MSAL_API_SCOPE],
|
|
redirectUri: MSAL_SILENT_REDIRECT_URI,
|
|
});
|
|
|
|
return response.accessToken;
|
|
}
|