Files
Iris/src/js/components/SpotifyAuthenticationFrame.js

126 lines
3.3 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
2016-10-26 08:51:28 +13:00
import { Link } from 'react-router'
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
import FontAwesome from 'react-fontawesome'
2016-10-26 08:51:28 +13:00
import Thumbnail from './Thumbnail'
2016-12-14 08:29:24 +13:00
import * as uiActions from '../services/ui/actions'
import * as spotifyActions from '../services/spotify/actions'
class SpotifyAuthenticationFrame extends React.Component{
constructor(props) {
super(props);
this.state = {
2017-04-22 20:39:29 +12:00
frameUrl: 'https://jamesbarnsley.co.nz/auth.php?action=frame',
authorizing: false
}
}
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
})
}
}, false);
}
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,
authorizing: true
})
}
renderAuthorizeButton(){
if( this.state.authorizing ){
return (
2017-05-19 08:59:29 +12:00
<button className="working">
Authorizing...
</button>
2017-05-19 08:59:29 +12:00
)
2016-11-25 22:49:34 +13:00
}else if( this.props.authorized ){
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
)
}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
)
}
}
renderRefreshButton(){
if (!this.props.authorized) return null
if (this.props.refreshing_token){
return (
2017-05-19 08:59:29 +12:00
<button className="working">
Refreshing...
</button>
);
} else {
return (
<button onClick={() => this.props.spotifyActions.refreshingToken()}>Force token refresh</button>
)
}
}
render(){
return (
<span>
2017-02-07 10:00:29 +13:00
<iframe src={this.state.frameUrl} style={{ display: 'none' }}></iframe>
{ this.renderAuthorizeButton() }
{ this.renderRefreshButton() }
</span>
);
}
}
const mapStateToProps = (state, ownProps) => {
2016-11-25 22:49:34 +13:00
return {
authorized: state.spotify.authorized,
authorizing: state.spotify.authorizing,
refreshing_token: state.spotify.refreshing_token
2016-11-25 22:49:34 +13:00
}
}
const mapDispatchToProps = (dispatch) => {
return {
2016-12-14 08:29:24 +13:00
uiActions: bindActionCreators(uiActions, dispatch),
spotifyActions: bindActionCreators(spotifyActions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(SpotifyAuthenticationFrame)