nativescript-spotify
Version:
Spotify for your NativeScript app.
225 lines • 9.37 kB
JavaScript
var observable_1 = require('data/observable');
var common_1 = require('../common');
var dialogs = require('ui/dialogs');
var app = require("application");
var appSettings = require("application-settings");
var http = require('http');
var AuthenticationRequest = com.spotify.sdk.android.authentication.AuthenticationRequest;
var AuthenticationResponse = com.spotify.sdk.android.authentication.AuthenticationResponse;
var AuthenticationClient = com.spotify.sdk.android.authentication.AuthenticationClient;
var TNSSpotifyAuth = (function () {
function TNSSpotifyAuth() {
}
TNSSpotifyAuth.prototype.setupEvents = function () {
TNSSpotifyAuth.instance = this;
this.setupNotifications();
};
TNSSpotifyAuth.LOGIN = function (showDialog) {
if (showDialog === void 0) { showDialog = true; }
var builder = new AuthenticationRequest.Builder(common_1.TNSSpotifyConstants.CLIENT_ID, AuthenticationResponse.Type.TOKEN, TNSSpotifyAuth.REDIRECT_URL);
builder.setShowDialog(showDialog);
builder.setScopes([
'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']);
var request = builder.build();
var activity = app.android.startActivity || app.android.foregroundActivity;
AuthenticationClient.openLoginActivity(activity, TNSSpotifyAuth.REQUEST_CODE, request);
app.android.on(app.AndroidApplication.activityResultEvent, (function (args) {
if (args.requestCode === TNSSpotifyAuth.REQUEST_CODE) {
var response = AuthenticationClient.getResponse(args.resultCode, args.intent);
var responseType = response.getType();
var error = {
msg: '',
code: undefined
};
if (responseType == AuthenticationResponse.Type.TOKEN) {
var token = response.getAccessToken();
TNSSpotifyAuth.SAVE_SESSION(token);
return;
}
else if (responseType === AuthenticationResponse.Type.ERROR) {
error.msg = response.getError();
console.log('err: ' + error.msg);
}
else if (responseType === AuthenticationResponse.Type.CODE) {
error.code = response.getCode();
console.log('code: ' + error.code);
}
else if (responseType === AuthenticationResponse.Type.EMPTY) {
error.msg = 'EMPTY';
console.log(error.msg);
}
else {
error.msg = 'UNKNOWN';
console.log(error.msg);
}
if (TNSSpotifyAuth.instance && TNSSpotifyAuth.instance.events) {
TNSSpotifyAuth.instance._authLoginError.data = error;
TNSSpotifyAuth.instance.events.notify(TNSSpotifyAuth.instance._authLoginError);
}
}
}));
};
TNSSpotifyAuth.LOGOUT = function () {
console.log("TNSSpotifyAuth.LOGOUT()");
TNSSpotifyAuth.SESSION = undefined;
if (TNSSpotifyAuth.instance && TNSSpotifyAuth.instance.events) {
TNSSpotifyAuth.instance._authLoginChange.data.status = false;
TNSSpotifyAuth.instance.events.notify(TNSSpotifyAuth.instance._authLoginChange);
}
};
TNSSpotifyAuth.HANDLE_AUTH_CALLBACK = function (url) {
if (TNSSpotifyAuth.instance && TNSSpotifyAuth.instance.events) {
TNSSpotifyAuth.instance.events.notify(TNSSpotifyAuth.instance._authLoginCheck);
}
};
TNSSpotifyAuth.LOGIN_WITH_SESSION = function (session) {
};
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 = sessionObj;
}
}
var showErrorAndLogout = function () {
console.log("showErrorAndLogout");
if (checkExisting) {
console.log("error renewing, calling TNSSpotifyAuth.LOGOUT()");
TNSSpotifyAuth.LOGOUT();
}
reject();
};
if (session) {
console.log('VALID.');
TNSSpotifyAuth.SESSION = session;
resolve();
}
else {
showErrorAndLogout();
}
});
};
TNSSpotifyAuth.SAVE_SESSION = function (session) {
console.log('SAVE_SESSION: ' + session);
TNSSpotifyAuth.SESSION = session;
appSettings.setString(common_1.TNSSpotifyConstants.KEY_STORE_SESSION, session);
if (TNSSpotifyAuth.instance && TNSSpotifyAuth.instance.events) {
TNSSpotifyAuth.instance.events.notify(TNSSpotifyAuth.instance._authLoginSuccess);
}
};
TNSSpotifyAuth.GET_STORED_SESSION = function () {
console.log('GET_STORED_SESSION: ' + TNSSpotifyAuth.SESSION);
return appSettings.getString(common_1.TNSSpotifyConstants.KEY_STORE_SESSION);
};
TNSSpotifyAuth.RENEW_SESSION = function (session) {
return new Promise(function (resolve, reject) {
console.log("trying to renew Spotify session...");
resolve();
});
};
TNSSpotifyAuth.CURRENT_USER = function () {
return new Promise(function (resolve, reject) {
http.request({
url: "https://api.spotify.com/v1/me",
method: 'GET',
headers: { "Content-Type": "application/json", "Authorization:": "Bearer " + TNSSpotifyAuth.SESSION }
}).then(function (res) {
if (res && res.content) {
var user = JSON.parse(res.content);
var product = 0;
switch (user.product) {
case 'unlimited':
product = 1;
break;
case 'premium':
product = 2;
break;
case 'unknown':
product = 3;
break;
}
var sUser = {
emailAddress: user.email,
displayName: user.display_name,
product: product,
uri: user.uri
};
resolve(sUser);
}
else {
reject();
}
}, function (err) {
console.log("current user error:", err);
for (var key in err) {
console.log("key: " + key, err[key]);
}
reject(err);
});
});
};
TNSSpotifyAuth.CHECK_PREMIUM = function () {
return new Promise(function (resolve, reject) {
TNSSpotifyAuth.CURRENT_USER().then(function (user) {
console.log("user.product:", user.product);
if (user) {
if (user.product == 1 || user.product == 2) {
resolve();
}
else {
setTimeout(function () {
dialogs.alert(TNSSpotifyAuth.PREMIUM_MSG);
}, 400);
TNSSpotifyAuth.CLEAR_COOKIES = true;
TNSSpotifyAuth.LOGOUT();
reject();
}
}
else {
reject();
}
}, reject);
});
};
TNSSpotifyAuth.prototype.setupNotifications = function () {
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
}
};
};
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.';
TNSSpotifyAuth.REQUEST_CODE = 1337;
return TNSSpotifyAuth;
}());
exports.TNSSpotifyAuth = TNSSpotifyAuth;
//# sourceMappingURL=auth.js.map