1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/components/ascribe_routes/auth_component.js
2015-10-06 15:38:39 +02:00

43 lines
972 B
JavaScript

'use strict';
import React from 'react';
import { History } from 'react-router';
import UserStore from '../../stores/user_store';
import UserActions from '../../actions/user_actions';
export function AuthComponent(Component) {
return React.createClass({
mixins: [History],
getInitialState() {
return UserStore.getState();
},
componentDidMount() {
UserStore.listen(this.onChange);
UserActions.fetchCurrentUser();
},
componentDidUpdate() {
if(this.state.currentUser && !this.state.currentUser.email) {
this.history.pushState(null, '/login');
}
},
componentWillUnmount() {
UserStore.unlisten(this.onChange);
},
onChange(state) {
this.setState(state);
},
render() {
return (
<Component {...this.props}/>
);
}
});
}