Using auth token

This commit is contained in:
James Barnsley
2016-10-17 14:27:48 +13:00
parent 4bff7ee0a9
commit 53a4743902
8 changed files with 76 additions and 40 deletions

View File

@ -48,3 +48,9 @@ export function loadArtist( uri ){
}
}
export function loadLibraryArtists(){
return {
type: 'SPOTIFY_LOAD_LIBRARY_ARTISTS'
}
}

View File

@ -11,13 +11,22 @@ const SpotifyMiddleware = (function(){
* @param data mixed = request payload
* @return ajax promise
**/
const sendRequest = (endpoint, method = 'GET', data = false) => {
return $.ajax({
const sendRequest = (token, endpoint, method = 'GET', data = false) => {
var options = {
method: method,
cache: true,
url: 'https://api.spotify.com/v1/'+endpoint,
data: data
});
};
if( token ){
options.headers = {
Authorization: 'Bearer '+ token
}
}
return $.ajax( options );
}
/**
@ -32,7 +41,7 @@ const SpotifyMiddleware = (function(){
store.dispatch({ type: 'SPOTIFY_ALBUM_LOADED', data: false })
var id = action.uri.replace('spotify:album:','');
sendRequest('albums/'+id)
sendRequest( false, 'albums/'+id )
.then( (response) => {
store.dispatch({ type: 'SPOTIFY_ALBUM_LOADED', data: response })
});
@ -44,13 +53,25 @@ const SpotifyMiddleware = (function(){
store.dispatch({ type: 'SPOTIFY_ARTIST_LOADED', data: false })
var id = action.uri.replace('spotify:artist:','');
sendRequest('artists/'+id)
sendRequest( false, 'artists/'+id )
.then( (response) => {
store.dispatch({ type: 'SPOTIFY_ARTIST_LOADED', data: response })
});
break;
case 'SPOTIFY_LOAD_LIBRARY_ARTISTS':
store.dispatch({ type: 'SPOTIFY_LIBRARY_ARTISTS_LOADED', data: false })
var token = store.getState().spotify.token;
sendRequest( token, 'me/following?type=artist' )
.then( (response) => {
store.dispatch({ type: 'SPOTIFY_LIBRARY_ARTISTS_LOADED', data: response })
});
break;
case 'SPOTIFY_CONNECT':
console.log('Spotify wants to connect')
break;

View File

@ -9,7 +9,7 @@ export default function reducer(spotify = {}, action){
return Object.assign({}, spotify, { authorizing: true });
case 'SPOTIFY_COMPLETE_AUTHORIZATION':
return Object.assign({}, spotify, { authorizing: false, authorization: action.data });
return Object.assign({}, spotify, { authorizing: false, authorization: action.data, token: action.data.access_token });
case 'SPOTIFY_REMOVE_AUTHORIZATION':
return Object.assign({}, spotify, { authorizing: false, authorization: false });
@ -27,7 +27,7 @@ export default function reducer(spotify = {}, action){
return Object.assign({}, spotify, { artist: action.data });
case 'SPOTIFY_LIBRARY_ARTISTS_LOADED':
return Object.assign({}, spotify, { library_artists: action.data });
return Object.assign({}, spotify, { libraryArtists: action.data });
default:
return spotify