UNPKG

@saphocom/auth0-plugin-cordova

Version:

auth0.js plugin for auth flow handling in Cordova environment.

158 lines (124 loc) 4.88 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _urlJoin = require('url-join'); var _urlJoin2 = _interopRequireDefault(_urlJoin); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var PopupHandler = function () { function PopupHandler(webAuth) { _classCallCheck(this, PopupHandler); this.webAuth = webAuth; this.options = null; this._currentPopup = null; } _createClass(PopupHandler, [{ key: 'preload', value: function preload(options) { var _this = this; var url = options.url || 'about:blank'; var popupOptions = options.popupOptions || {}; popupOptions.location = 'yes'; delete popupOptions.width; delete popupOptions.height; var windowFeatures = Object.keys(popupOptions).map(function (key) { return key + '=' + popupOptions[key]; }).join(','); if (this._currentPopup && !this._currentPopup.closed) { return this._currentPopup; } this._currentPopup = window.open(url, '_blank', windowFeatures); this._currentPopup.kill = function (success) { _this._currentPopup.success = success; this.close(); _this._currentPopup = null; }; return this._currentPopup; } }, { key: 'load', value: function load(url, _, options, cb) { var _this = this; this.url = url; this.options = options; if (!this._currentPopup) { options.url = url; this.preload(options); } else { this._currentPopup.location.href = url; } this.transientErrorHandler = function (event) { _this.errorHandler(event, cb); }; this.transientStartHandler = function (event) { _this.startHandler(event, cb); }; this.transientExitHandler = function () { _this.exitHandler(cb); }; this._currentPopup.addEventListener('loaderror', this.transientErrorHandler); this._currentPopup.addEventListener('loadstart', this.transientStartHandler); this._currentPopup.addEventListener('exit', this.transientExitHandler); } }, { key: 'errorHandler', value: function errorHandler(event, cb) { if (!this._currentPopup) { return; } this._currentPopup.kill(true); cb({ error: 'window_error', errorDescription: event.message }); } }, { key: 'unhook', value: function unhook() { this._currentPopup.removeEventListener('loaderror', this.transientErrorHandler); this._currentPopup.removeEventListener('loadstart', this.transientStartHandler); this._currentPopup.removeEventListener('exit', this.transientExitHandler); } }, { key: 'exitHandler', value: function exitHandler(cb) { if (!this._currentPopup) { return; } // when the modal is closed, this event is called which ends up removing the // event listeners. If you move this before closing the modal, it will add ~1 sec // delay between the user being redirected to the callback and the popup gets closed. this.unhook(); if (!this._currentPopup.success) { cb({ error: 'window_closed', errorDescription: 'Browser window closed' }); } } }, { key: 'startHandler', value: function startHandler(event, cb) { var _this = this; if (!this._currentPopup) { return; } var callbackUrl = (0, _urlJoin2.default)('https:', this.webAuth.baseOptions.domain, '/mobile'); if (event.url && !(event.url.indexOf(callbackUrl + '#') === 0)) { return; } var parts = event.url.split('#'); if (parts.length === 1) { return; } var opts = { hash: parts.pop(), _idTokenVerification: false }; if (this.options.nonce) { opts.nonce = this.options.nonce; } this.webAuth.parseHash(opts, function (error, result) { if (error || result) { _this._currentPopup.kill(true); cb(error, result); } }); } }]); return PopupHandler; }(); exports.default = PopupHandler;