LastFM middleware; Window.onmessage needs origin to prevent clashing between auth popups

This commit is contained in:
James Barnsley
2017-11-03 09:06:41 +13:00
parent 35a61686b3
commit a102c31ff6
10 changed files with 186 additions and 58 deletions

View File

@ -97,6 +97,7 @@ const CoreMiddleware = (function(){
case 'CORE_START_SERVICES':
store.dispatch(mopidyActions.connect());
store.dispatch(pusherActions.connect());
store.dispatch(lastfmActions.connect());
next(action)
break

View File

@ -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' })
}
}
)
}

View 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

View File

@ -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

View File

@ -167,6 +167,17 @@ const localstorageMiddleware = (function(){
);
localStorage.setItem('ui', JSON.stringify(ui))
break
case 'LASTFM_AUTHORIZATION_GRANTED':
var lastfm = JSON.parse(localStorage.getItem('lastfm') );
lastfm = Object.assign(
{},
{
session: action.data.session
}
);
localStorage.setItem('lastfm', JSON.stringify(lastfm));
break;
}
}