Skip to content

Commit

Permalink
Merge pull request #204 from thisissoon/feature/fix-spotify-auth-toke…
Browse files Browse the repository at this point in the history
…n-when-expired

Fix: spotify auth when has expired
  • Loading branch information
edoparearyee committed Aug 10, 2015
2 parents 0cc54f4 + ab69b20 commit 116ec49
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 65 deletions.
87 changes: 25 additions & 62 deletions app/js/auth/services/SpotifyAuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,67 +96,26 @@ angular.module("FM.auth.SpotifyAuthService", [

Spotify.getCurrentUser()
.then(function (response){
user = response;
deferred.resolve(response);
})
.catch(function (response){
user = null;
deferred.reject(response);
if (response && !response.error){
user = response;
deferred.resolve(response);
} else {
user = null;
deferred.reject(response);
}
});

return deferred.promise;
};

/**
* Authenticate with spotify OAuth
* @private
* @method authenticateOnlyIfToken
* @return {Object} Spotify user object
*/
var authenticateOnlyIfToken = function authenticateOnlyIfToken() {
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 (response){

if (response && response.error && response.error.status === 401){
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
* @param {Boolean} onlyIfToken only request auth if user token exists
* @return {Object} Spotify user object
*/
this.authenticate = function authenticate() {
this.authenticate = function authenticate(onlyIfToken) {
var deferred = $q.defer(),
authToken = $window.localStorage.getItem(TOKEN_NAME);

Expand All @@ -182,15 +141,19 @@ angular.module("FM.auth.SpotifyAuthService", [
});

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

return deferred.promise;
Expand All @@ -215,7 +178,7 @@ angular.module("FM.auth.SpotifyAuthService", [
this.getUser = function getUser() {
var deferred = $q.defer();

authenticateOnlyIfToken()
_this.authenticate(true)
.then(function (){
getCurrentUser()
.then(deferred.resolve)
Expand Down Expand Up @@ -254,7 +217,7 @@ angular.module("FM.auth.SpotifyAuthService", [
* @method init
*/
this.init = function init() {
authenticateOnlyIfToken();
_this.authenticate(true);
};

}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/auth/services/SpotifyAuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("FM.auth.SpotifyAuthService", function() {
playlists = [{ id: "123" }, { id: "456" }];

userPlaylistRequest = $httpBackend.whenGET(/.*api.spotify.com\/v1\/users\/.*\/playlists/);
userRequest = $httpBackend.whenGET(/.*api.spotify.com\/v1\/me.*/);
userRequest = $httpBackend.whenGET(/.*api.spotify.com\/v1\/me/);

userRequest.respond(200, user);
userPlaylistRequest.respond(200, playlists);
Expand Down Expand Up @@ -170,7 +170,7 @@ describe("FM.auth.SpotifyAuthService", function() {
// user has local token, token is expired, token is renewed
var error = { error: { status: 401 } };
token = "foo";
userRequest.respond(401, error);
userRequest.respond(200, error);
var setAuthTokenSpy = spyOn(Spotify, "setAuthToken");
var alertSpy = spyOn(AlertService, "set");

Expand All @@ -194,7 +194,7 @@ describe("FM.auth.SpotifyAuthService", function() {
expect(service.isAuthenticated()).toBeTruthy();

// user has local token, token is expired, token renewal fails
userRequest.respond(401, error);
userRequest.respond(200, error);
spotifyLoginResponse = error;

service.authenticate();
Expand Down

0 comments on commit 116ec49

Please sign in to comment.