nativescript-ssoauth
Version:
An advanced webview using Chrome CustomTabs on Android and SFSafariViewController on iOS.
117 lines (116 loc) • 6.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var color_1 = require("tns-core-modules/color");
var utils = require("tns-core-modules/utils/utils");
var ssoauth_common_1 = require("./ssoauth.common");
var kCloseSafariViewControllerNotification = 'kCloseSafariViewControllerNotification';
var SFSafariViewControllerDelegateImpl = (function (_super) {
__extends(SFSafariViewControllerDelegateImpl, _super);
function SFSafariViewControllerDelegateImpl() {
return _super !== null && _super.apply(this, arguments) || this;
}
SFSafariViewControllerDelegateImpl.initWithOwnerCallback = function (owner) {
var delegate = SFSafariViewControllerDelegateImpl.new();
delegate._owner = owner;
return delegate;
};
SFSafariViewControllerDelegateImpl.prototype.safariViewControllerDidCompleteInitialLoad = function (controller, didLoadSuccessfully) {
console.log('Delegate, safariViewControllerDidCompleteInitialLoad: ' + didLoadSuccessfully);
};
SFSafariViewControllerDelegateImpl.prototype.safariViewControllerDidFinish = function (controller) {
console.log("Delegate, safariViewControllerDidFinish");
};
SFSafariViewControllerDelegateImpl.ObjCProtocols = [SFSafariViewControllerDelegate];
return SFSafariViewControllerDelegateImpl;
}(NSObject));
var AuthSFSafariViewController = (function (_super) {
__extends(AuthSFSafariViewController, _super);
function AuthSFSafariViewController() {
return _super !== null && _super.apply(this, arguments) || this;
}
AuthSFSafariViewController.initWithOptions = function (options) {
var SFViewController = AuthSFSafariViewController.alloc().initWithURL(NSURL.URLWithString(options.url));
AuthSFSafariViewController._isClosedManually = true;
AuthSFSafariViewController._options = options;
AuthSFSafariViewController._onManualClose = options.onManualClose ? options.onManualClose : null;
AuthSFSafariViewController._successCompletionHandler = options.successCompletionHandler
? options.successCompletionHandler
: null;
return SFViewController;
};
AuthSFSafariViewController.prototype.loginSafari = function (notification) {
AuthSFSafariViewController._successUrl = notification.object;
AuthSFSafariViewController._isClosedManually = false;
var sharedApp = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
sharedApp.keyWindow.rootViewController.dismissViewControllerAnimatedCompletion(true, null);
utils.ios
.getter(NSNotificationCenter, NSNotificationCenter.defaultCenter)
.removeObserver(AuthSFSafariViewController._observer);
utils.GC();
};
AuthSFSafariViewController.prototype.viewDidAppear = function (animated) {
_super.prototype.viewDidAppear.call(this, animated);
console.log("View did appear !");
if (AuthSFSafariViewController._options.isLogout) {
var sharedApp = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
sharedApp.keyWindow.rootViewController.dismissViewControllerAnimatedCompletion(true, null);
AuthSFSafariViewController._successCompletionHandler(null);
}
if (AuthSFSafariViewController._successCompletionHandler &&
typeof AuthSFSafariViewController._successCompletionHandler === 'function') {
AuthSFSafariViewController._observer = utils.ios
.getter(NSNotificationCenter, NSNotificationCenter.defaultCenter)
.addObserverForNameObjectQueueUsingBlock(kCloseSafariViewControllerNotification, null, null, this.loginSafari);
}
};
AuthSFSafariViewController.prototype.viewDidDisappear = function () {
_super.prototype.viewDidDisappear.call(this, true);
console.log("View did disappear !");
if (AuthSFSafariViewController._onManualClose &&
typeof AuthSFSafariViewController._onManualClose === 'function' &&
AuthSFSafariViewController._isClosedManually) {
AuthSFSafariViewController._onManualClose(true);
}
else if (AuthSFSafariViewController._successCompletionHandler &&
typeof AuthSFSafariViewController._successCompletionHandler === 'function' &&
!AuthSFSafariViewController._isClosedManually) {
AuthSFSafariViewController._successCompletionHandler(AuthSFSafariViewController._successUrl);
}
if (AuthSFSafariViewController._observer) {
utils.ios
.getter(NSNotificationCenter, NSNotificationCenter.defaultCenter)
.removeObserver(AuthSFSafariViewController._observer);
}
};
return AuthSFSafariViewController;
}(SFSafariViewController));
function init() { }
exports.init = init;
function SSOAuthOpenUrl(options) {
if (!options.url) {
throw new Error('No url set in the Advanced WebView Options object.');
}
var sfc = AuthSFSafariViewController.initWithOptions(options);
if (options.toolbarColor) {
sfc.preferredBarTintColor = new color_1.Color(options.toolbarColor).ios;
}
if (options.toolbarControlsColor) {
sfc.preferredControlTintColor = new color_1.Color(options.toolbarControlsColor).ios;
}
sfc.delegate = SFSafariViewControllerDelegateImpl.initWithOwnerCallback(new WeakRef({}));
var app = utils.ios.getter(UIApplication, UIApplication.sharedApplication);
var animated = true;
var completionHandler = null;
app.keyWindow.rootViewController.presentViewControllerAnimatedCompletion(sfc, animated, completionHandler);
}
exports.SSOAuthOpenUrl = SSOAuthOpenUrl;
function SSOAuthOpenUrlPostNotification(url) {
utils.ios
.getter(NSNotificationCenter, NSNotificationCenter.defaultCenter)
.postNotificationNameObject(kCloseSafariViewControllerNotification, url.absoluteString);
}
exports.SSOAuthOpenUrlPostNotification = SSOAuthOpenUrlPostNotification;
function SSOAuthExtractAppUrl(url) {
return ssoauth_common_1.extractAppURL(url);
}
exports.SSOAuthExtractAppUrl = SSOAuthExtractAppUrl;