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.

This function acquiresTokenSilent which it uses to renew the token for me automatically?

See original GitHub issue

Core Library

MSAL.js v2 (@azure/msal-browser)

Core Library Version

2

Wrapper Library

Not Applicable

Wrapper Library Version

2

Description

I need to know if this is the right code so that after my token expires, call a refresh token to not stop my app.

MSAL Configuration

//MSAL configuration
const msalConfig = {
    auth: {
        clientId: '7e391644-15a5-47d4-9cd6-5d86711f4c66',
        authority: 'https://login.microsoftonline.com/92d143fd-f58c-420c-8469-393fab93e1ea',
        redirectUri: 'http://localhost:8080'
    }
};
const msalRequest = { scopes: ['user.read'] };
function ensureScope (scope) {
    if (!msalRequest.scopes.some((s) => s.toLowerCase() === scope.toLowerCase())) {
        msalRequest.scopes.push(scope);
    }
}
// Inicializa a Biblioteca MSAL.
const msalClient = new msal.PublicClientApplication(msalConfig);

// Conecta o usuário.
async function signIn() {
    const authResult = await msalClient.loginPopup(msalRequest);
    sessionStorage.setItem('msalAccount', authResult.account.username);
}
// Manipula a obtendo um token de acesso que Microsoft Graph pode usar.
async function getToken() {
    let account = sessionStorage.getItem('msalAccount');
    if (!account) {
        throw new Error(
            'As informações do usuário foram apagadas da sessão. Saia e faça login novamente.');
    }
    try {
        // Primeiro, tente obter o token silenciosamente
        const silentRequest = {
            scopes: msalRequest.scopes,
            account: msalClient.getAccountByUsername(account)
        };

        const silentResult = await msalClient.acquireTokenSilent(silentRequest);
        return silentResult.accessToken;
    } catch (silentError) {

        // Se as solicitações silenciosas falharem com InteractionRequiredAuthError,
        // tenta obter o token interativamente

        if (silentError instanceof msal.InteractionRequiredAuthError) {
            const interactiveResult = await msalClient.acquireTokenPopup(msalRequest);
            return interactiveResult.accessToken;
        } else {
            throw silentError;
        }
    }
}

Relevant Code Snippets

No response

Identity Provider

Azure AD / MSA

Source

Internal (Microsoft)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
gustavoabelcommented, Feb 11, 2022

@bmahall my goal is to bring a refresh token to my app, so when my access token expires it calls another one… because when my app has an expired token I can’t access the database.

0reactions
msftbot[bot]commented, Mar 6, 2022

@gustavoabell This issue has been automatically marked as stale because it is marked as requiring author feedback but has not had any activity for 5 days. If your issue has been resolved please let us know by closing the issue. If your issue has not been resolved please leave a comment to keep this open. It will be closed automatically in 7 days if it remains stale.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Single-page application: Acquire a token to call an API
js is to first attempt a silent token request by using the acquireTokenSilent method. When this method is called, the library first checks...
Read more >
How do I renew idToken and accessToken after one hour so ...
I think once you call login function, you can use acquireTokenSilent() . However, you should set a fallback, because, if the user account ......
Read more >
How to use refresh token coming from acquiretoken silent in ...
Refresh token is used to refresh access token, you can see this doc, it won't refresh the access token automatically. You can decode...
Read more >
Calling Microsoft Graph using @azure/msal-react from PCF ...
When we use the acquireTokenSilent method, it handles the renewal of these tokens automatically when expired before returning the access token.
Read more >
angular acquire token silently to refresh token - You.com
You could use acquireTokenSilent method to automatically refresh token for you in MSAL.NET. MSAL uses a cache to store tokens based on specific...
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