LastFM middleware; Window.onmessage needs origin to prevent clashing between auth popups
This commit is contained in:
@ -59,8 +59,11 @@ export function set(data){
|
||||
**/
|
||||
|
||||
export function authorizationGranted(data){
|
||||
data.token_expiry = new Date().getTime() + data.expires_in;
|
||||
return { type: 'LASTFM_AUTHORIZATION_GRANTED', data: data }
|
||||
data.session.expiry = new Date().getTime() + 3600;
|
||||
return {
|
||||
type: 'LASTFM_AUTHORIZATION_GRANTED',
|
||||
data: data
|
||||
}
|
||||
}
|
||||
|
||||
export function revokeAuthorization(){
|
||||
@ -70,12 +73,37 @@ export function revokeAuthorization(){
|
||||
export function connect(){
|
||||
return (dispatch, getState) => {
|
||||
|
||||
dispatch({ type: 'LASTFM_CONNECTING' })
|
||||
dispatch({ type: 'LASTFM_CONNECTING' });
|
||||
|
||||
sendRequest(dispatch, getState, 'method=artist.getInfo&artist=')
|
||||
// Authorized, dual-purpose our connection to get the current user
|
||||
if (getState().lastfm.session){
|
||||
dispatch(getMe());
|
||||
|
||||
// Not authorized, just use a generic lookup to test our connection
|
||||
} else {
|
||||
sendRequest(dispatch, getState, 'method=artist.getInfo&artist=')
|
||||
.then(
|
||||
response => {
|
||||
dispatch({ type: 'LASTFM_CONNECTED' })
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getMe(){
|
||||
return (dispatch, getState) => {
|
||||
var params = 'method=user.getInfo&user='+getState().lastfm.session.name
|
||||
sendRequest(dispatch, getState, params)
|
||||
.then(
|
||||
response => {
|
||||
dispatch({ type: 'LASTFM_CONNECTED' })
|
||||
if (response.user){
|
||||
dispatch({
|
||||
type: 'LASTFM_USER_LOADED',
|
||||
user: response.user
|
||||
});
|
||||
dispatch({ type: 'LASTFM_CONNECTED' })
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
43
src/js/services/lastfm/middleware.js
Executable file
43
src/js/services/lastfm/middleware.js
Executable file
@ -0,0 +1,43 @@
|
||||
|
||||
import ReactGA from 'react-ga'
|
||||
|
||||
var helpers = require('./../../helpers')
|
||||
var lastfmActions = require('./actions')
|
||||
var uiActions = require('../ui/actions')
|
||||
var pusherActions = require('../pusher/actions')
|
||||
|
||||
const LastfmMiddleware = (function(){
|
||||
|
||||
/**
|
||||
* The actual middleware inteceptor
|
||||
**/
|
||||
return store => next => action => {
|
||||
var state = store.getState();
|
||||
|
||||
switch(action.type){
|
||||
|
||||
case 'LASTFM_USER_LOADED':
|
||||
var user = Object.assign(
|
||||
{},
|
||||
action.user,
|
||||
{
|
||||
uri: "lastfm:user:"+action.user.name
|
||||
}
|
||||
);
|
||||
store.dispatch({
|
||||
type: "USER_LOADED",
|
||||
key: user.uri,
|
||||
user: user
|
||||
});
|
||||
next(action);
|
||||
break;
|
||||
|
||||
// This action is irrelevant to us, pass it on to the next middleware
|
||||
default:
|
||||
return next(action);
|
||||
}
|
||||
}
|
||||
|
||||
})();
|
||||
|
||||
export default LastfmMiddleware
|
||||
@ -13,22 +13,17 @@ export default function reducer(lastfm = {}, action){
|
||||
return Object.assign({}, lastfm, action.data)
|
||||
|
||||
case 'LASTFM_AUTHORIZATION_GRANTED':
|
||||
return Object.assign({}, lastfm, {
|
||||
enabled: true,
|
||||
return Object.assign({}, lastfm, {
|
||||
authorizing: false,
|
||||
authorization: action.data,
|
||||
api_key: action.data.api_key,
|
||||
token_expiry: action.data.token_expiry
|
||||
session: action.data.session
|
||||
})
|
||||
|
||||
case 'LASTFM_AUTHORIZATION_REVOKED':
|
||||
return Object.assign({}, lastfm, {
|
||||
authorizing: false,
|
||||
authorization: false,
|
||||
api_key: false,
|
||||
token_expiry: 0,
|
||||
authorizing: false,
|
||||
session: false,
|
||||
me: false
|
||||
})
|
||||
});
|
||||
|
||||
default:
|
||||
return lastfm
|
||||
|
||||
Reference in New Issue
Block a user