2016-10-16 12:37:37 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
|
|
|
|
import { connect } from 'react-redux'
|
|
|
|
|
import { createStore, bindActionCreators } from 'redux'
|
2016-10-17 10:39:11 +13:00
|
|
|
import FontAwesome from 'react-fontawesome'
|
2016-10-16 12:37:37 +13:00
|
|
|
|
|
|
|
|
import * as actions from '../services/spotify/actions'
|
|
|
|
|
|
|
|
|
|
class SpotifyAuthenticationFrame extends React.Component{
|
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 10:39:11 +13:00
|
|
|
componentDidMount(){
|
|
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
|
|
// listen for incoming messages from the authorization iframe
|
|
|
|
|
// this is triggered when authentication is granted from the popup
|
|
|
|
|
window.addEventListener('message', function(event){
|
|
|
|
|
|
|
|
|
|
// only allow incoming data from our authorized authenticator proxy
|
|
|
|
|
if( !/^https?:\/\/jamesbarnsley\.co\.nz/.test(event.origin) ) return false;
|
|
|
|
|
|
|
|
|
|
var data = JSON.parse(event.data);
|
2016-10-19 14:09:34 +13:00
|
|
|
self.props.actions.authorizationGranted( data );
|
2016-10-17 10:39:11 +13:00
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 14:09:34 +13:00
|
|
|
renderMe(){
|
|
|
|
|
if( this.props.spotify.me ){
|
|
|
|
|
if( this.props.spotify.me.display_name ){
|
|
|
|
|
return <span>Logged in as { this.props.spotify.me.display_name }</span>
|
|
|
|
|
}else{
|
|
|
|
|
return <span>Logged in as { this.props.spotify.me.id }</span>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 10:39:11 +13:00
|
|
|
renderAuthorizeButton(){
|
|
|
|
|
if( this.props.spotify.authorizing ){
|
|
|
|
|
return (
|
|
|
|
|
<span>
|
|
|
|
|
<FontAwesome name="circle-o-notch" spin />
|
|
|
|
|
<span>Authorizing...</span>
|
|
|
|
|
</span>
|
|
|
|
|
);
|
2016-10-18 16:53:33 +13:00
|
|
|
}else if( this.props.spotify.authorized ){
|
2016-10-17 10:39:11 +13:00
|
|
|
return (
|
2016-10-19 14:09:34 +13:00
|
|
|
<div>
|
|
|
|
|
<button onClick={() => this.props.actions.removeAuthorization()}>Log out</button>
|
|
|
|
|
{ this.renderMe() }
|
|
|
|
|
</div>
|
2016-10-17 10:39:11 +13:00
|
|
|
);
|
|
|
|
|
}else{
|
|
|
|
|
return (
|
|
|
|
|
<button onClick={() => this.props.actions.startAuthorization()}>Log in</button>
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-10-16 12:37:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render(){
|
|
|
|
|
var src = '//jamesbarnsley.co.nz/spotmop.php?action=frame';
|
|
|
|
|
if( this.props.spotify.authorizing ){
|
|
|
|
|
src = '//jamesbarnsley.co.nz/spotmop.php?action=authorize&app='+location.protocol+'//'+window.location.host;
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2016-10-17 10:39:11 +13:00
|
|
|
{ this.renderAuthorizeButton() }
|
2016-10-17 14:27:48 +13:00
|
|
|
<iframe src={src} style={{ display: 'none' }}></iframe>
|
2016-10-16 12:37:37 +13:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
|
return {
|
|
|
|
|
actions: bindActionCreators(actions, dispatch)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(SpotifyAuthenticationFrame)
|