Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Fix dialog/authentication issues for Edge + 365 and Desktop versions of Office #144

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
18 changes: 17 additions & 1 deletion src/helpers/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export class Dialog<T> {
else if (Utilities.isAddin) {
this._result = this._addinDialog();
}
else if (Utilities.isEdge) {
this._result = this._edgeDialog();
}
else {
this._result = this._webDialog();
}
Expand Down Expand Up @@ -124,7 +127,7 @@ export class Dialog<T> {
try {
const options = 'width=' + this.size.width + ',height=' + this.size.height + this._windowFeatures;
window.open(this.url, this.url, options);
if (Utilities.isIEOrEdge) {
if (Utilities.isIE) {
this._pollLocalStorageForToken(resolve, reject);
}
else {
Expand All @@ -143,6 +146,19 @@ export class Dialog<T> {
});
}

private _edgeDialog(): Promise<T> {
return new Promise((resolve, reject) => {
Office.context.ui.displayDialogAsync(this.url, { width: this.size.width$, height: this.size.height$ }, (result: Office.AsyncResult) => {
if (result.status === Office.AsyncResultStatus.Failed) {
reject(new DialogError(result.error.message, result.error));
}
else {
this._pollLocalStorageForToken(resolve, reject);
}
});
});
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a bug with this dialog on the Outlook desktop client (tested build 1908 & 1909). I use the fork this PR is based on, and my changes in my fork of the fork(🙄) seems to have fixed it.

The problem seem to be that the dialog isn't properly closed as far as office.js is concerned. It works as expected when using OWA in Edge, but not on the desktop client.

The scenario is the user is authenticated through the popup dialog right. And at some point the token is invalid and offce-helpers tries to open a new popup to authenticate the user again. But it receives an error from office.js with code 12007 "A dialog box is already opened from this host window."

These changes seems to have fixed the problem:

   private _edgeDialog(): Promise<T> {
     return new Promise((resolve, reject) => {
       Office.context.ui.displayDialogAsync(this.url, { width: this.size.width$, height: this.size.height$ }, (result: Office.AsyncResult) => {
+        let dialog = result.value as Office.DialogHandler;
+
         if (result.status === Office.AsyncResultStatus.Failed) {
           reject(new DialogError(result.error.message, result.error));
         }
         else {
-          this._pollLocalStorageForToken(resolve, reject);
+          const onToken = token => {
+            if (dialog) {
+              dialog.close();
+            }
+
+            return resolve(token);
+          };
+
+          this._pollLocalStorageForToken(onToken, reject);
         }
       });
     });


private _pollLocalStorageForToken(resolve: (value: T) => void, reject: (reason: DialogError) => void) {
localStorage.removeItem(Dialog.key);
const POLL_INTERVAL = 400;
Expand Down
18 changes: 16 additions & 2 deletions src/helpers/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,28 @@ export class Utilities {
* Utility to check if the code is running inside of an add-in.
*/
static get isAddin() {
return Utilities.host !== HostType.WEB;
return Utilities.host !== HostType.WEB && !Utilities.isEdge;
}

/**
* Utility to check if the browser is IE11.
*/
static get isIE() {
return /Trident\//gi.test(window.navigator.userAgent);
}

/**
* Utility to check if the browser is Edge.
*/
static get isEdge() {
return /Edge\//gi.test(window.navigator.userAgent);
}

/**
* Utility to check if the browser is IE11 or Edge.
*/
static get isIEOrEdge() {
return /Edge\/|Trident\//gi.test(window.navigator.userAgent);
return Utilities.isIE || Utilities.isEdge;
}

/**
Expand Down