I’d like to create an Promise that can subscribe to an InAppBrowserEvent
. I use InAppBrowser
to pass credentials from a login page to secure storage using localStorage
as the surrogate, but what I would love to do is let the end user consume a Promise
once the browser has ended (exit
event), so the call would be something like:
this.auth.authenticate().then(() =>).catch(() =>);
I have the following, and don’t know how to wrap it correctly to return a Promise
:
public authenticate() {
let authEndpoint = [this.endpoint, '/#', this.appIdentifier].join('');
this.browser = new InAppBrowser(authEndpoint, "_blank", Configuration.INAPPBROWSER_OPTIONS);
// How can I return a Promise resolve when 'exit' is completed?
let exit = this.browser.on("exit").subscribe(event => this.handleOnExit(event));
let loadstart = this.browser.on("loadstart").subscribe(event => this.handleOnLoadStart(event));
let loadstop = this.browser.on("loadstop").subscribe(event => this.handleOnLoadStop(event));
}
private handleOnExit(event: InAppBrowserEvent, callback: Function) {
return callback();
}
/**
*
*/
private handleOnLoadStart(event: InAppBrowserEvent) {
}
/**
*
*/
private handleOnLoadStop(event: InAppBrowserEvent)
this.browser.executeScript({ code: `localStorage.removeItem("${Configuration.AUTH_KEY}");` });
this.browser.show();
let poll = setInterval(() => {
this.browser.executeScript({ code: `localStorage.getItem("${Configuration.AUTH_KEY}");` })
.then(args => {
if (args[0]) {
clearInterval(poll);
this._tokens = JSON.stringify(args[0]);
this.browser.executeScript({ code: `localStorage.removeItem("${Configuration.AUTH_KEY}");` });
this.browser.close();
}
});
}, 300);
}
Any place to start would help out and would be appreciated.