@saphocom/auth0-plugin-cordova
Version:
auth0.js plugin for auth flow handling in Cordova environment.
121 lines (94 loc) • 3.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _onCustomSchemaRedirect = null;
/**
* @param {WebAuth} webAuth
* @constructor
*/
var BrowserHandler = function () {
function BrowserHandler(webAuth) {
_classCallCheck(this, BrowserHandler);
this.webAuth = webAuth;
}
/**
* @param {string} url
* @param {string} relayUrl
* @param {Object} popOpts
* @param {Function} cb
*/
_createClass(BrowserHandler, [{
key: 'load',
value: function load(url, relayUrl, popOpts, cb) {
var _this = this;
_onCustomSchemaRedirect = function _onCustomSchemaRedirect() {
cb(null, {}); //finish & close Lock modal
};
if (_typeof(window.SafariViewController) !== 'object') {
this._openSystemBrowser(url);
return;
}
window.SafariViewController.isAvailable(function (isAvailable) {
if (!isAvailable) {
_this._openSystemBrowser(url);
return;
}
var browser = _this._openBrowserSession(url, function (error, result) {
if (error != null) {
cb(error);
}
if (result.event === 'closed') {
cb(null, {});
}
});
_onCustomSchemaRedirect = function _onCustomSchemaRedirect() {
browser.hide();
// it shows up in `authenticated` event handler and can tend to use data here but the only reliable code point to sign-in is custom-schema handler
cb(null, {}); //finish & close Lock modal
};
});
}
}, {
key: '_openBrowserSession',
/**
* @param {string} url
* @param {Function} cb
* @returns {SafariViewController} Cordova Plugin
* @private
*/
value: function _openBrowserSession(url, cb) {
var browser = window.SafariViewController;
var options = {
hidden: false,
url: url
};
browser.show(options, function (result) {
cb(null, result);
}, function (message) {
cb(new Error(message));
});
return browser;
}
/**
* @param {string} url
* @private
*/
}, {
key: '_openSystemBrowser',
value: function _openSystemBrowser(url) {
window.open(url, '_system');
}
}], [{
key: 'finishAuth',
value: function finishAuth() {
//cordova app state not lost? then finish Lock session
_onCustomSchemaRedirect && _onCustomSchemaRedirect();
}
}]);
return BrowserHandler;
}();
exports.default = BrowserHandler;