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

126 lines
3.1 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'
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'
import * as actions from '../services/spotify/actions'
class SpotifyAuthenticationFrame extends React.Component{
constructor(props) {
super(props);
this.state = {
2016-10-31 09:25:15 +13:00
frameUrl: '//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){
2016-10-31 09:25:15 +13:00
if(event.data == 'closed'){
self.setState({
frameUrl: '//jamesbarnsley.co.nz/auth.php?action=frame',
authorizing: false
})
}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);
self.props.actions.authorizationGranted( data );
self.props.actions.getMe();
// and turn off our authorizing switch
self.setState({
frameUrl: '//jamesbarnsley.co.nz/auth.php?action=frame',
authorizing: false
})
}
}, false);
}
startAuthorization(){
this.setState({
2016-10-31 09:25:15 +13:00
frameUrl: '//jamesbarnsley.co.nz/auth.php?action=authorize&app='+location.protocol+'//'+window.location.host,
authorizing: true
})
}
renderAuthorizeButton(){
if( this.state.authorizing ){
return (
<button disabled>
<FontAwesome name="circle-o-notch" spin />
2016-10-26 21:20:48 +13:00
&nbsp;
Authorizing...
</button>
);
2016-11-25 22:49:34 +13:00
}else if( this.props.authorized ){
return (
2016-12-12 16:16:17 +13:00
<button className="destructive" onClick={() => this.props.actions.authorizationRevoked()}>Log out</button>
);
}else{
return (
2016-12-12 16:16:17 +13:00
<button className="primary" onClick={() => this.startAuthorization()}>Log in</button>
);
}
}
renderRefreshButton(){
2016-11-25 22:49:34 +13:00
if( !this.props.authorized ) return null;
2016-11-25 22:49:34 +13:00
if( this.props.refreshing_token ){
return (
<button disabled>
<FontAwesome name="circle-o-notch" spin />
2016-10-26 21:20:48 +13:00
&nbsp;
Refreshing...
</button>
);
}else{
return (
2016-10-26 08:51:28 +13:00
<button onClick={() => this.props.actions.refreshingToken()}>Refresh token</button>
);
}
}
render(){
return (
<span>
{ this.renderAuthorizeButton() }
2016-11-25 22:49:34 +13:00
&nbsp;&nbsp;
{ this.renderRefreshButton() }
<iframe src={this.state.frameUrl} style={{ display: 'none' }}></iframe>
</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 {
actions: bindActionCreators(actions, dispatch)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(SpotifyAuthenticationFrame)