1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-20 17:51:25 +01:00
onion/js/components/ascribe_routes/auth_component.js

44 lines
1013 B
JavaScript
Raw Normal View History

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