Forum Discussion
authentication.authenticate() opens in external browser tab instead of pop-up window on first call
- Jun 22, 2020
The problem was that .initialize() method is not a synchronous operation. Digging into the API docs shows that .initialize() takes a callback function as a parameter. So wrapping this in a promise and chaining the authentication call to happen afterward fixed the problem.
The problem was that .initialize() method is not a synchronous operation. Digging into the API docs shows that .initialize() takes a callback function as a parameter. So wrapping this in a promise and chaining the authentication call to happen afterward fixed the problem.
- matt131Oct 06, 2020Copper Contributor
Anything else special that you did to get this to work in generarl? Everything works as expected in the web version of Teams. However in the desktop client it always opens in an external browser. I tried waiting for intialization like below:
microsoftTeams.initialize(() => { microsoftTeams.authentication.authenticate({ url: myUrl, ......
However this completley breaks the call and it no longer even opens in an external browser. As you mentioned debugging is impossible in the desktop client. Any tips?
- RobDiazMarinoNov 09, 2020Copper Contributor
matt131 The code you posted seems like it should work, so I'm not sure exactly what's wrong. I'll share a little bit more about how we set up our code and maybe it will help.
We created our own wrapper for the MS Teams API like so (there is a lot more to it but I've extracted only the parts pertinent to this problem):import * as microsoftTeams from '@microsoft/teams-js' let initializePromise = null export default { init() { if (!initializePromise) { initializePromise = new Promise((resolve) => { microsoftTeams.initialize(resolve) }) } return initializePromise }, authenticate() { return this.init().then(() => { return new Promise((resolve, reject) => { // Launch the MS Teams Login flow microsoftTeams.authentication.authenticate({ url: window.location.origin + 'loginpath', successCallback: resolve, failureCallback: reject, width: 500, height: 500 }) }).catch(error => { console.error('Error during Teams authentication: ', error) throw error }) }) } }
Setting up our init function in this way means we can call it at the beginning of any operation that requires the MS Teams API to ensure it has been initialized properly first. If it has already been called once then it resolves immediately from then on.
Hope this helps!- Amey2020Dec 17, 2020Copper Contributor
RobDiazMarino, matt131 were you able to solve the problem of pop up working only in the web App and not on the desktop App?
I am facing the same problem and not finding a solution.
Note: I have tried the approach of ensuring microsoft.intialize is success before calling the authenticate API.