2016-10-16 12:37:37 +13:00
|
|
|
|
|
|
|
|
import React, { PropTypes } from 'react'
|
|
|
|
|
import { connect } from 'react-redux'
|
2016-10-26 08:51:28 +13:00
|
|
|
import { Link } from 'react-router'
|
2016-10-16 12:37:37 +13:00
|
|
|
import { createStore, bindActionCreators } from 'redux'
|
2017-01-04 15:24:13 +13:00
|
|
|
import ReactGA from 'react-ga'
|
2016-10-26 08:51:28 +13:00
|
|
|
|
2016-10-17 10:39:11 +13:00
|
|
|
import FontAwesome from 'react-fontawesome'
|
2016-10-26 08:51:28 +13:00
|
|
|
import Thumbnail from './Thumbnail'
|
2016-10-16 12:37:37 +13:00
|
|
|
|
2016-12-14 08:29:24 +13:00
|
|
|
import * as uiActions from '../services/ui/actions'
|
|
|
|
|
import * as spotifyActions from '../services/spotify/actions'
|
2016-10-16 12:37:37 +13:00
|
|
|
|
|
|
|
|
class SpotifyAuthenticationFrame extends React.Component{
|
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
2016-10-21 09:56:07 +13:00
|
|
|
|
|
|
|
|
this.state = {
|
2017-04-22 20:39:29 +12:00
|
|
|
frameUrl: 'https://jamesbarnsley.co.nz/auth.php?action=frame',
|
2016-10-21 09:56:07 +13:00
|
|
|
authorizing: false
|
|
|
|
|
}
|
2016-10-16 12:37:37 +13:00
|
|
|
}
|
|
|
|
|
|
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){
|
2017-01-05 21:13:37 +13:00
|
|
|
|
2016-10-31 09:25:15 +13:00
|
|
|
if(event.data == 'closed'){
|
|
|
|
|
self.setState({
|
2017-04-22 20:39:29 +12:00
|
|
|
frameUrl: 'https://jamesbarnsley.co.nz/auth.php?action=frame',
|
2016-10-31 09:25:15 +13:00
|
|
|
authorizing: false
|
|
|
|
|
})
|
2017-01-05 21:13:37 +13:00
|
|
|
}else if(event.data == 'blocked'){
|
|
|
|
|
self.props.uiActions.createNotification('Popup blocked. Please allow popups and try again.','bad')
|
2016-10-31 09:25:15 +13:00
|
|
|
}else{
|
|
|
|
|
|
|
|
|
|
// 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-12-14 08:29:24 +13:00
|
|
|
self.props.spotifyActions.authorizationGranted( data );
|
|
|
|
|
self.props.spotifyActions.getMe();
|
2016-10-31 09:25:15 +13:00
|
|
|
|
|
|
|
|
// and turn off our authorizing switch
|
|
|
|
|
self.setState({
|
2017-04-22 20:39:29 +12:00
|
|
|
frameUrl: 'https://jamesbarnsley.co.nz/auth.php?action=frame',
|
2016-10-31 09:25:15 +13:00
|
|
|
authorizing: false
|
|
|
|
|
})
|
|
|
|
|
}
|
2016-10-17 10:39:11 +13:00
|
|
|
|
|
|
|
|
}, false);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-21 09:56:07 +13:00
|
|
|
startAuthorization(){
|
|
|
|
|
this.setState({
|
2017-04-22 20:39:29 +12:00
|
|
|
frameUrl: 'https://jamesbarnsley.co.nz/auth.php?action=authorize&app='+location.protocol+'//'+window.location.host,
|
2016-10-21 09:56:07 +13:00
|
|
|
authorizing: true
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 10:39:11 +13:00
|
|
|
renderAuthorizeButton(){
|
2016-10-21 09:56:07 +13:00
|
|
|
if( this.state.authorizing ){
|
2016-10-17 10:39:11 +13:00
|
|
|
return (
|
2017-05-19 08:59:29 +12:00
|
|
|
<button className="working">
|
2016-10-21 09:56:07 +13:00
|
|
|
Authorizing...
|
|
|
|
|
</button>
|
2017-05-19 08:59:29 +12:00
|
|
|
)
|
2016-11-25 22:49:34 +13:00
|
|
|
}else if( this.props.authorized ){
|
2016-10-17 10:39:11 +13:00
|
|
|
return (
|
2016-12-14 08:29:24 +13:00
|
|
|
<button className="destructive" onClick={() => this.props.spotifyActions.authorizationRevoked()}>Log out</button>
|
2017-05-19 08:59:29 +12:00
|
|
|
)
|
2016-10-17 10:39:11 +13:00
|
|
|
}else{
|
|
|
|
|
return (
|
2016-12-12 16:16:17 +13:00
|
|
|
<button className="primary" onClick={() => this.startAuthorization()}>Log in</button>
|
2017-05-19 08:59:29 +12:00
|
|
|
)
|
2016-10-17 10:39:11 +13:00
|
|
|
}
|
2016-10-16 12:37:37 +13:00
|
|
|
}
|
|
|
|
|
|
2016-10-21 09:56:07 +13:00
|
|
|
renderRefreshButton(){
|
2017-04-23 10:32:18 +12:00
|
|
|
if (!this.props.authorized) return null
|
2016-10-21 09:56:07 +13:00
|
|
|
|
2017-04-23 10:32:18 +12:00
|
|
|
if (this.props.refreshing_token){
|
2016-10-21 09:56:07 +13:00
|
|
|
return (
|
2017-05-19 08:59:29 +12:00
|
|
|
<button className="working">
|
2016-10-21 09:56:07 +13:00
|
|
|
Refreshing...
|
|
|
|
|
</button>
|
|
|
|
|
);
|
2017-04-23 10:32:18 +12:00
|
|
|
} else {
|
2016-10-21 09:56:07 +13:00
|
|
|
return (
|
2017-04-23 10:32:18 +12:00
|
|
|
<button onClick={() => this.props.spotifyActions.refreshingToken()}>Force token refresh</button>
|
|
|
|
|
)
|
2016-10-16 12:37:37 +13:00
|
|
|
}
|
2016-10-21 09:56:07 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render(){
|
2016-10-16 12:37:37 +13:00
|
|
|
return (
|
2016-12-12 09:07:47 +13:00
|
|
|
<span>
|
2017-02-07 10:00:29 +13:00
|
|
|
<iframe src={this.state.frameUrl} style={{ display: 'none' }}></iframe>
|
2016-10-17 10:39:11 +13:00
|
|
|
{ this.renderAuthorizeButton() }
|
2016-10-21 09:56:07 +13:00
|
|
|
{ this.renderRefreshButton() }
|
2016-12-12 09:07:47 +13:00
|
|
|
</span>
|
2016-10-16 12:37:37 +13:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2016-11-25 22:49:34 +13:00
|
|
|
return {
|
|
|
|
|
authorized: state.spotify.authorized,
|
|
|
|
|
authorizing: state.spotify.authorizing,
|
2016-11-29 15:36:01 +13:00
|
|
|
refreshing_token: state.spotify.refreshing_token
|
2016-11-25 22:49:34 +13:00
|
|
|
}
|
2016-10-16 12:37:37 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
|
return {
|
2016-12-14 08:29:24 +13:00
|
|
|
uiActions: bindActionCreators(uiActions, dispatch),
|
|
|
|
|
spotifyActions: bindActionCreators(spotifyActions, dispatch)
|
2016-10-16 12:37:37 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(SpotifyAuthenticationFrame)
|