OfficeDev / teams-toolkit

Developer tools for building Teams apps
Other
470 stars 195 forks source link

MS Teams Desktop tabs auth-start.html popup flashes on tab open #12129

Open jurisjansons opened 4 months ago

jurisjansons commented 4 months ago

Describe the bug Moving between MS Teams triggers auth-start.html popop to open every time, even when switching from tab to tab. I was sent here from teams-js issue: https://github.com/OfficeDev/microsoft-teams-library-js/issues/1994.

More details, videos and teams-js teams discoveries etc. can be found there.

Issue can be observer on Windows, both MS Teams Desktop and Web versions

To Reproduce Steps to reproduce the behavior:

  1. Install app in personal scope https://teams.microsoft.com/l/app/12f1d10a-936d-41d0-8e87-9d0758bfdde0?source=app-details-dialog
  2. Switch between tabs in app
  3. Notice popup flashing

Expected behavior Switching tabs or reopening tab later doesn't trigger popup and gets token from cache instead

Screenshots Video with issue: https://github.com/OfficeDev/microsoft-teams-library-js/assets/10829036/9f4996a5-fba5-4d1f-b962-795a9a8cce45

KennethBWSong commented 3 months ago

Hi @jurisjansons Thank you for using Teams Toolkit. From the log I guess publicClientApplication is not correctly initialized in TeamsFx SDK when upgrading msal-browser from 2.x to 3.x. I will check for more detail and reach out to you when we have any update.

jurisjansons commented 3 months ago

Mr. Bot, what kind of author feedback where you expecting

KennethBWSong commented 3 months ago

@jurisjansons Sorry for late reply. We have fixed this issue. Would you please have a try with the latest beta version?

jurisjansons commented 3 months ago

Still getting same errors and popup on every tab change.

[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Info - Create teams user credential useTeamsUserCredential.js:22 
[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Verbose - Validate authentication configuration useTeamsUserCredential.js:22 
[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Info - Get access token with scopes: User.Read User.ReadBasic.All useTeamsUserCredential.js:22 
[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Verbose - Get SSO token from memory cache useTeamsUserCredential.js:22 
[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Verbose - Failed to call acquireTokenSilent. Reason: uninitialized_public_client_application: You must call and await the initialize function before attempting to call any other MSAL API.  For more visit: aka.ms/msaljs/browser-errors. useTeamsUserCredential.js:22 
[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Verbose - Failed to call ssoSilent. Reason: uninitialized_public_client_application: You must call and await the initialize function before attempting to call any other MSAL API.  For more visit: aka.ms/msaljs/browser-errors. useTeamsUserCredential.js:22 
[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Error - Failed to get access token cache silently, please login first: you need login first before get access token. App.tsx:67 
ErrorWithCode.UiRequiredError: Failed to get access token cache silently, please login first: you need login first before get access token.
    at TeamsUserCredential.getToken (teamsUserCredential.browser.ts:272:1)
    at async App.tsx:64:1
App.tsx:68 
[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Info - Popup login page to get user's access token with scopes: User.Read User.ReadBasic.All useTeamsUserCredential.js:22 
[Thu, 22 Aug 2024 12:37:06 GMT] : @microsoft/teamsfx : Error - Consent failed for the scope User.Read User.ReadBasic.All with error: Get empty response. App.tsx:69 
ErrorWithCode.ConsentFailed: Consent failed for the scope User.Read User.ReadBasic.All with error: Get empty response.
    at TeamsUserCredential.login (https://app-intervy-tabs-dev.azurewebsites.net/static/js/bundle.js:83661:13)
    at async https://app-intervy-tabs-dev.azurewebsites.net/static/js/bundle.js:103:7

What library version teams-js (2.21.0) and msal-browser (2.35.0) I should be using in auth-start.html? Or they doesnt affect anything?

KennethBWSong commented 3 months ago

@jurisjansons I don't think this error comes from the auth-start.html. Would you please check whether you are using @microsoft/teamsfx-react package and update the version to latest beta version if using this package?

jurisjansons commented 1 month ago

Went away for a bit, now I'm back and still having same issue. Updated teams-fx to latest beta version as of today: 2.3.3-beta.2024091206.0

Error log:

[Mon, 30 Sep 2024 07:45:56 GMT] : @microsoft/teamsfx : Info - Create teams user credential
useTeamsUserCredential.js:22 [Mon, 30 Sep 2024 07:45:56 GMT] : @microsoft/teamsfx : Verbose - Validate authentication configuration
useTeamsUserCredential.js:22 [Mon, 30 Sep 2024 07:45:56 GMT] : @microsoft/teamsfx : Info - Get access token with scopes: User.Read User.ReadBasic.All
useTeamsUserCredential.js:22 [Mon, 30 Sep 2024 07:45:56 GMT] : @microsoft/teamsfx : Verbose - Get SSO token from memory cache
useTeamsUserCredential.js:22 [Mon, 30 Sep 2024 07:45:56 GMT] : @microsoft/teamsfx : Verbose - Failed to call acquireTokenSilent. Reason: no_account_error: No account object provided to acquireTokenSilent and no active account has been set. Please call setActiveAccount or provide an account on the request.. 
useTeamsUserCredential.js:22 [Mon, 30 Sep 2024 07:46:06 GMT] : @microsoft/teamsfx : Verbose - Failed to call ssoSilent. Reason: monitor_window_timeout: Token acquisition in iframe failed due to timeout.  For more visit: aka.ms/msaljs/browser-errors. 
useTeamsUserCredential.js:22 [Mon, 30 Sep 2024 07:46:06 GMT] : @microsoft/teamsfx : Error - Failed to get access token cache silently, please login first: you need login first before get access token.
App.tsx:67 ErrorWithCode.UiRequiredError: Failed to get access token cache silently, please login first: you need login first before get access token.
    at TeamsUserCredential.getToken (teamsUserCredential.browser.ts:272:1)
    at async App.tsx:64:1
App.tsx:68 [Mon, 30 Sep 2024 07:46:06 GMT] : @microsoft/teamsfx : Info - Popup login page to get user's access token with scopes: User.Read User.ReadBasic.All
useTeamsUserCredential.js:22 [Mon, 30 Sep 2024 07:46:07 GMT] : @microsoft/teamsfx : Error - Consent failed for the scope User.Read User.ReadBasic.All with error: Get empty response.
jurisjansons commented 1 month ago

Noticed that while waiting for await teamsUserCredential.getToken(scopes), some requests in the background are successfully fetched, where I'm using @microsoft/teams-js and calling await authentication.getAuthToken() to get token.

For now changed my code like this:

    try {
      // await teamsUserCredential.getToken(scopes);
      await authentication.getAuthToken();
      Providers.globalProvider.setState(ProviderState.SignedIn);
    } catch (error) {
      console.log(error);
      await teamsUserCredential.login(scopes).catch((error) => {
        console.error(error);
      });
      Providers.globalProvider.setState(ProviderState.SignedIn);
    }

to avoid popup.

Is this correct approach? Lack of examples is confusing or maybe you could point me to up to date example where authentication is implemented in MS Teams tab with react?

KennethBWSong commented 1 month ago

@jurisjansons I do believe your code above is correct. You can refer to our sample here. In this sample, we implement OBO flow for authentication.

jurisjansons commented 1 month ago

@KennethBWSong sample uses teamsUserCredential.getToken(scopes) which creates huge delay (~5s), fails to get token from cache and calls teamsUserCredential.login(scopes) from catch (error) and again creates delay (~5s) on every tab open.

Probably will leave this open, currently managed to fix this, but can't get it working with teamsUserCredential.getToken(scopes)

KennethBWSong commented 1 month ago

@jurisjansons Thanks for your info. Actually, in teamsUserCredential.getToken(scopes), teamsfx sdk will leverage msal.acquireTokenSilent(), msal.ssoSilent() to get token from cache/silent SSO. We will investigate this issue to see if we can improve the experience.

jurisjansons commented 1 month ago

There is another underlying issue related to this that I and other people have: https://github.com/microsoftgraph/microsoft-graph-toolkit/issues/3160.

KennethBWSong commented 1 month ago

@jurisjansons Thanks for your info. We will investigate and let you know as soon as we have any updates.

kvergins commented 1 week ago

Any updates? I have the same issue.

KennethBWSong commented 1 week ago

@kvergins We are still working on this issue. Will let you know when we have any update.