UNPKG

@salte-auth/popup

Version:

A Salte Auth provider for authenticating via Popup!

62 lines (51 loc) 1.86 kB
import { Handler, Utils, SalteAuthError } from '@salte-auth/salte-auth'; class Popup extends Handler { constructor(config) { super(config); this.config = 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 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 = Utils.URL.parse(popupWindow.location); popupWindow.close(); resolve(parsed); } clearInterval(checker); } catch (error) { if (Utils.Events.isCrossDomainError(error)) return; popupWindow.close(); clearInterval(checker); reject(error); } }, 100); }); } } export { Popup };