Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: spotify auth when has expired #204

Merged
merged 4 commits into from
Aug 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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