Skip to content

Commit

Permalink
Merge pull request #194 from thisissoon/feature/user-spotify-playlists
Browse files Browse the repository at this point in the history
Feature: User spotify playlists
  • Loading branch information
edoparearyee committed Jul 31, 2015
2 parents ccc4110 + fa126bc commit f567128
Show file tree
Hide file tree
Showing 17 changed files with 654 additions and 89 deletions.
3 changes: 3 additions & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<script src="js/search/controllers/TrackSearchCtrl.js"></script>
<script src="js/search/directives/artist.js"></script>
<script src="js/search/directives/album.js"></script>
<script src="js/search/directives/playlist.js"></script>

<script src="js/nav/nav.js"></script>

Expand All @@ -151,6 +152,8 @@

<script src="js/users/users.js"></script>
<script src="js/users/controllers/UserStatsCtrl.js"></script>
<script src="js/users/controllers/UserPlaylistsCtrl.js"></script>
<script src="js/users/controllers/UserPlaylistDetailCtrl.js"></script>

<script src="js/app.js"></script>
<!-- /build -->
Expand Down
189 changes: 155 additions & 34 deletions app/js/auth/services/SpotifyAuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,45 +48,36 @@ angular.module("FM.auth.SpotifyAuthService", [

/**
* Spotify user object
* @public
* @private
* @property {Object} user
*/
this.user = null;
var user = null;

/**
* Whether the user is authenticated
* @public
* @private
* @property {Object} authenticated
*/
this.authenticated = false;
var authenticated = false;

/**
* @public
* @method isAuthenticated
* @return {Boolean} whether the current user is authenticated
*/
this.isAuthenticated = function isAuthenticated() {
return _this.authenticated;
};

/**
* Authenticate with spotify OAuth
* @method authenticate
* @return {Object} Spotify user object
* @private
* @method login
* @return {Promise} Spotify auth token
*/
this.authenticate = function authenticate() {
var login = function login(){
var deferred = $q.defer();

Spotify.login()
.then(function (response){
if (response && typeof response === "string"){
Spotify.setAuthToken(response);
$window.localStorage.setItem(TOKEN_NAME, response);
_this.authenticated = true;
authenticated = true;
deferred.resolve(response);
} else {
AlertService.set(ERRORS.AUTH_VALIDATION_TITLE, response.error.status);
_this.authenticated = false;
authenticated = false;
$window.localStorage.removeItem(TOKEN_NAME);
deferred.reject(response);
}
Expand All @@ -96,44 +87,174 @@ angular.module("FM.auth.SpotifyAuthService", [
};

/**
* @private
* @method getCurrentUser
* @return {Object} current spotify user object
* @return {Promise} current spotify user object
*/
this.getCurrentUser = function getCurrentUser(){
var getCurrentUser = function getCurrentUser(){
var deferred = $q.defer();

Spotify.getCurrentUser()
.then(function (response){
_this.user = response;
_this.authenticated = true;
user = response;
deferred.resolve(response);
})
.catch(function (response){
_this.user = null;
_this.authenticated = false;
user = null;
deferred.reject(response);
});

return deferred.promise;
};

/**
* Attempt to authenicate the user if
* they have logged into spotify before
* @public
* @method init
* Authenticate with spotify OAuth
* @private
* @method authenticateOnlyIfToken
* @return {Object} Spotify user object
*/
this.init = function init() {
var authToken = $window.localStorage.getItem(TOKEN_NAME);
var authenticateOnlyIfToken = function authenticateOnlyIfToken() {
var deferred = $q.defer(),
authToken = $window.localStorage.getItem(TOKEN_NAME);

// check for existing auth token
if (authToken){
Spotify.setAuthToken(authToken);
_this.getCurrentUser()
.then(function (response){

getCurrentUser()
.then(function (){
authenticated = true;
deferred.resolve(authToken);
})
.catch(function (response){

if (response && response.error && response.error.status === 401){
_this.authenticate();
login()
.then(function (response){
authenticated = true;
deferred.resolve(response);
})
.catch(function (response){
authenticated = false;
deferred.reject(response);
});
} else {
deferred.reject(response);
}
});

} else {
deferred.reject();
}

return deferred.promise;
};

/**
* Authenticate with spotify OAuth
* @public
* @method authenticate
* @return {Object} Spotify user object
*/
this.authenticate = function authenticate() {
var deferred = $q.defer(),
authToken = $window.localStorage.getItem(TOKEN_NAME);

// check for existing auth token
if (authToken){
Spotify.setAuthToken(authToken);

getCurrentUser()
.then(function (){
authenticated = true;
deferred.resolve(authToken);
})
.catch(function (){
login()
.then(function (response){
authenticated = true;
deferred.resolve(response);
})
.catch(function (response){
authenticated = false;
deferred.reject(response);
});
});

} else {
login()
.then(function (response){
authenticated = true;
deferred.resolve(response);
})
.catch(function (response){
authenticated = false;
deferred.reject(response);
});
}

return deferred.promise;
};

/**
* @public
* @method isAuthenticated
* @return {Boolean} whether the current user is authenticated
*/
this.isAuthenticated = function isAuthenticated() {
return authenticated;
};

/**
* Attempt to get the current user object and request authentication
* if token has expired.
* @public
* @method getUser
* @return {Promise} User object
*/
this.getUser = function getUser() {
var deferred = $q.defer();

authenticateOnlyIfToken()
.then(function (){
getCurrentUser()
.then(deferred.resolve)
.catch(deferred.reject);
})
.catch(deferred.reject);

return deferred.promise;
};

/**
* Get Users Playlists
* @public
* @method getUserPlaylists
* @param {Object} request options
* @return {Promise} User playlists object
*/
this.getUserPlaylists = function getUserPlaylists(options) {
var deferred = $q.defer();

_this.getUser()
.then(function (response){
Spotify.getUserPlaylists(response.id, options)
.then(deferred.resolve)
.catch(deferred.reject);
})
.catch(deferred.reject);

return deferred.promise;
};

/**
* Attempt to authenicate the user if
* they have logged into spotify before
* @public
* @method init
*/
this.init = function init() {
authenticateOnlyIfToken();
};

}
Expand Down
26 changes: 26 additions & 0 deletions app/js/search/directives/playlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";
/**
* A directive for the spotify playlist object
* @module FM.search.playlistDirective
* @author SOON_
*/
angular.module("FM.search.playlistDirective", [

])
/**
* @example
<fm-playlist data-playlist="playlistObj"></fm-playlist>
* @constructor
* @class fmPlaylist
*/
.directive("fmPlaylist", [
function (){
return {
restrict: "EA",
scope: {
playlist: "="
},
templateUrl: "partials/search/playlist.html"
};
}
]);
4 changes: 3 additions & 1 deletion app/js/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @requires FM.search.TrackSearchCtrl
* @requires FM.search.artistDirective
* @requires FM.search.albumDirective
* @requires FM.search.playlistDirective
*/
angular.module("FM.search", [
"FM.search.SearchCtrl",
Expand All @@ -21,7 +22,8 @@ angular.module("FM.search", [
"FM.search.AlbumDetailCtrl",
"FM.search.TrackSearchCtrl",
"FM.search.artistDirective",
"FM.search.albumDirective"
"FM.search.albumDirective",
"FM.search.playlistDirective"
])
/**
* @method run
Expand Down
Loading

0 comments on commit f567128

Please sign in to comment.