@salte-auth/popup
Version:
A Salte Auth provider for authenticating via Popup!
66 lines (53 loc) • 1.97 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var salteAuth = require('@salte-auth/salte-auth');
class Popup extends salteAuth.Handler {
constructor(config) {
super(config);
this.config = salteAuth.Utils.Common.defaults(this.config, {
window: {
name: '@salte-auth/popup',
height: 600,
width: 600
}
});
}
get name() {
return 'popup';
}
get auto() {
return false;
}
async open(options) {
var top = window.innerHeight / 2 - this.config.window.height / 2 + window.screenTop;
var left = window.innerWidth / 2 - this.config.window.width / 2 + window.screenLeft;
var popupWindow = window.open(options.url, this.config.window.name, "height=".concat(this.config.window.height, ", width=").concat(this.config.window.width, ", status=yes, toolbar=no, menubar=no, location=no, top=").concat(top, ", left=").concat(left));
if (!popupWindow) {
throw new salteAuth.SalteAuthError({
message: 'We were unable to open the popup window, its likely that the request was blocked.',
code: 'popup_blocked'
});
}
popupWindow.focus(); // TODO: Find a better way of tracking when a Window closes.
return new Promise((resolve, reject) => {
var checker = setInterval(() => {
try {
if (!popupWindow.closed) {
// This could throw cross-domain errors, so we need to silence them.
if (popupWindow.location.href.indexOf(options.redirectUrl) !== 0) return;
var parsed = salteAuth.Utils.URL.parse(popupWindow.location);
popupWindow.close();
resolve(parsed);
}
clearInterval(checker);
} catch (error) {
if (salteAuth.Utils.Events.isCrossDomainError(error)) return;
popupWindow.close();
clearInterval(checker);
reject(error);
}
}, 100);
});
}
}
exports.Popup = Popup;