nativescript-spotify
Version:
Spotify for your NativeScript app.
248 lines • 11.2 kB
JavaScript
var observable_1 = require('data/observable');
var dialogs = require('ui/dialogs');
var common_1 = require('../common');
var notification_1 = require('./notification');
var TNSSpotifyAuthDelegate = (function (_super) {
__extends(TNSSpotifyAuthDelegate, _super);
function TNSSpotifyAuthDelegate() {
_super.apply(this, arguments);
}
TNSSpotifyAuthDelegate.prototype.authenticationViewControllerDidLoginWithSession = function (ctrl, session) {
console.log("DidLoginWithSession!");
TNSSpotifyAuth.LOGIN_WITH_SESSION(session);
};
TNSSpotifyAuthDelegate.prototype.authenticationViewControllerDidFailToLogin = function (ctrl, error) {
console.log(error);
NSNotificationCenter.defaultCenter.postNotificationNameObject(common_1.TNSSpotifyConstants.NOTIFY_LOGIN_ERROR, error);
};
TNSSpotifyAuthDelegate.prototype.authenticationViewControllerDidCancelLogin = function (ctrl) {
console.log('User canceled login.');
};
TNSSpotifyAuthDelegate.ObjCProtocols = [SPTAuthViewDelegate];
return TNSSpotifyAuthDelegate;
}(NSObject));
var TNSSpotifyAuth = (function (_super) {
__extends(TNSSpotifyAuth, _super);
function TNSSpotifyAuth() {
_super.apply(this, arguments);
}
TNSSpotifyAuth.prototype.setupEvents = function () {
this.setupNotifications();
};
TNSSpotifyAuth.LOGIN = function () {
SPTAuth.defaultInstance().clientID = common_1.TNSSpotifyConstants.CLIENT_ID;
SPTAuth.defaultInstance().redirectURL = NSURL.URLWithString(TNSSpotifyAuth.REDIRECT_URL);
SPTAuth.defaultInstance().requestedScopes = [
'streaming',
'user-read-private',
'user-read-email',
'user-library-modify',
'user-library-read',
'playlist-read-private',
'playlist-modify-private',
'playlist-modify-public',
'playlist-read-collaborative'];
TNSSpotifyAuth.AUTH_VIEW_SHOWING = true;
var authvc = SPTAuthViewController.authenticationViewController();
authvc.delegate = new TNSSpotifyAuthDelegate();
if (TNSSpotifyAuth.CLEAR_COOKIES) {
authvc.clearCookies(function () { return 1; });
}
authvc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
authvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
var rootview = UIApplication.sharedApplication.keyWindow.rootViewController;
rootview.modalPresentationStyle = UIModalPresentationCurrentContext;
rootview.definesPresentationContext = true;
rootview.presentViewControllerAnimatedCompletion(authvc, true, null);
};
TNSSpotifyAuth.LOGOUT = function () {
TNSSpotifyAuth.SESSION = undefined;
var userDefaults = NSUserDefaults.standardUserDefaults;
userDefaults.removeObjectForKey(common_1.TNSSpotifyConstants.KEY_STORE_SESSION);
userDefaults.synchronize();
NSNotificationCenter.defaultCenter.postNotificationNameObject(common_1.TNSSpotifyConstants.NOTIFY_AUTH_LOGIN_CHANGE, false);
};
TNSSpotifyAuth.HANDLE_AUTH_CALLBACK = function (url) {
NSNotificationCenter.defaultCenter.postNotificationNameObject(common_1.TNSSpotifyConstants.NOTIFY_LOGIN_CHECK, null);
if (SPTAuth.defaultInstance().canHandleURL(url)) {
SPTAuth.defaultInstance().handleAuthCallbackWithTriggeredAuthURLCallback(url, function (error, session) {
if (error != null) {
console.log("*** Auth error: " + error);
return;
}
if (TNSSpotifyAuth.AUTH_VIEW_SHOWING) {
var rootview = UIApplication.sharedApplication.keyWindow.rootViewController;
rootview.dismissViewControllerAnimatedCompletion(true, null);
TNSSpotifyAuth.AUTH_VIEW_SHOWING = false;
}
TNSSpotifyAuth.SAVE_SESSION(session);
NSNotificationCenter.defaultCenter.postNotificationNameObject(common_1.TNSSpotifyConstants.NOTIFY_LOGIN_SUCCESS, null);
return true;
});
}
};
TNSSpotifyAuth.LOGIN_WITH_SESSION = function (session) {
TNSSpotifyAuth.CLEAR_COOKIES = false;
TNSSpotifyAuth.SAVE_SESSION(session);
NSNotificationCenter.defaultCenter.postNotificationNameObject(common_1.TNSSpotifyConstants.NOTIFY_LOGIN_SUCCESS, null);
};
TNSSpotifyAuth.VERIFY_SESSION = function (session) {
return new Promise(function (resolve, reject) {
console.log("verifying Spotify session...");
var checkExisting = true;
if (!session) {
checkExisting = false;
var sessionObj = TNSSpotifyAuth.GET_STORED_SESSION();
if (sessionObj) {
session = NSKeyedUnarchiver.unarchiveObjectWithData(sessionObj);
}
}
var showErrorAndLogout = function () {
console.log("showErrorAndLogout");
if (checkExisting) {
console.log("error renewing, calling TNSSpotifyAuth.LOGOUT()");
TNSSpotifyAuth.LOGOUT();
}
reject();
};
if (session) {
if (!session.isValid()) {
console.log('NOT VALID.');
TNSSpotifyAuth.RENEW_SESSION(session).then(resolve, function () {
showErrorAndLogout();
});
}
else {
console.log('VALID.');
TNSSpotifyAuth.SESSION = session;
resolve();
}
}
else {
showErrorAndLogout();
}
});
};
TNSSpotifyAuth.SAVE_SESSION = function (session) {
TNSSpotifyAuth.SESSION = session;
var userDefaults = NSUserDefaults.standardUserDefaults;
var sessionData = NSKeyedArchiver.archivedDataWithRootObject(session);
userDefaults.setObjectForKey(sessionData, common_1.TNSSpotifyConstants.KEY_STORE_SESSION);
userDefaults.synchronize();
};
TNSSpotifyAuth.GET_STORED_SESSION = function () {
var userDefaults = NSUserDefaults.standardUserDefaults;
return userDefaults.objectForKey(common_1.TNSSpotifyConstants.KEY_STORE_SESSION);
};
TNSSpotifyAuth.RENEW_SESSION = function (session) {
return new Promise(function (resolve, reject) {
console.log("trying to renew Spotify session...");
SPTAuth.defaultInstance().renewSessionCallback(session, function (error, session) {
if (error != null) {
console.log("*** Renew session error: " + error);
reject();
return;
}
console.log('RENEWED.');
TNSSpotifyAuth.SAVE_SESSION(session);
resolve();
});
});
};
TNSSpotifyAuth.CURRENT_USER = function () {
return new Promise(function (resolve, reject) {
if (TNSSpotifyAuth.SESSION) {
SPTUser.requestCurrentUserWithAccessTokenCallback(TNSSpotifyAuth.SESSION.accessToken, function (error, user) {
if (error != null) {
console.log("*** Request current user error: " + error);
reject();
return;
}
var sUser = {
emailAddress: user.emailAddress,
displayName: user.displayName,
product: user.product,
uri: user.uri
};
resolve(sUser);
});
}
else {
reject();
}
});
};
TNSSpotifyAuth.CHECK_PREMIUM = function () {
return new Promise(function (resolve, reject) {
TNSSpotifyAuth.CURRENT_USER().then(function (user) {
if (user && user.product) {
console.log("User Product: " + user.product);
if (user.product == 0 || user.product == 3) {
setTimeout(function () {
dialogs.alert(TNSSpotifyAuth.PREMIUM_MSG);
}, 400);
TNSSpotifyAuth.CLEAR_COOKIES = true;
TNSSpotifyAuth.LOGOUT();
reject();
}
else {
resolve();
}
}
else {
reject();
}
}, reject);
});
};
TNSSpotifyAuth.prototype.setupNotifications = function () {
var _this = this;
this._observers = new Array();
this.events = new observable_1.Observable();
this._authLoginCheck = {
eventName: 'authLoginCheck',
object: this.events
};
this._authLoginSuccess = {
eventName: 'authLoginSuccess',
object: this.events
};
this._authLoginError = {
eventName: 'authLoginError',
object: this.events,
data: {}
};
this._authLoginChange = {
eventName: 'authLoginChange',
object: this.events,
data: {
status: false
}
};
this.addNotificationObserver(common_1.TNSSpotifyConstants.NOTIFY_LOGIN_CHECK, function (notification) {
_this.events.notify(_this._authLoginCheck);
});
this.addNotificationObserver(common_1.TNSSpotifyConstants.NOTIFY_LOGIN_SUCCESS, function (notification) {
_this.events.notify(_this._authLoginSuccess);
});
this.addNotificationObserver(common_1.TNSSpotifyConstants.NOTIFY_LOGIN_ERROR, function (notification) {
_this._authLoginError.data = notification.object;
_this.events.notify(_this._authLoginError);
});
this.addNotificationObserver(common_1.TNSSpotifyConstants.NOTIFY_AUTH_LOGIN_CHANGE, function (notification) {
_this._authLoginChange.data.status = notification.object;
_this.events.notify(_this._authLoginChange);
});
};
TNSSpotifyAuth.prototype.addNotificationObserver = function (notificationName, onReceiveCallback) {
var observer = notification_1.TNSSpotifyNotificationObserver.new().initWithCallback(onReceiveCallback);
NSNotificationCenter.defaultCenter.addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
this._observers.push(observer);
return observer;
};
TNSSpotifyAuth.CLEAR_COOKIES = false;
TNSSpotifyAuth.PREMIUM_MSG = 'We are so sorry! Music streaming on mobile requires a Spotify Premium account. You can check out more at http://spotify.com. Last time we checked it was $9.99/month with the first 30 days free.';
return TNSSpotifyAuth;
}(NSObject));
exports.TNSSpotifyAuth = TNSSpotifyAuth;
//# sourceMappingURL=auth.js.map