You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.1 KiB
49 lines
1.1 KiB
import React from 'react'; |
|
import LoginStore from '../stores/LoginStore'; |
|
|
|
export default (ComposedComponent) => { |
|
return class AuthenticatedComponent extends React.Component { |
|
|
|
static willTransitionTo(transition) { |
|
if (!LoginStore.isLoggedIn()) { |
|
transition.redirect('/login', {}, {'nextPath' : transition.path}); |
|
} |
|
} |
|
|
|
constructor() { |
|
super() |
|
this.state = this._getLoginState(); |
|
} |
|
|
|
_getLoginState() { |
|
return { |
|
userLoggedIn: LoginStore.isLoggedIn(), |
|
user: LoginStore.user, |
|
jwt: LoginStore.jwt |
|
}; |
|
} |
|
|
|
componentDidMount() { |
|
this.changeListener = this._onChange.bind(this); |
|
LoginStore.addChangeListener(this.changeListener); |
|
} |
|
|
|
_onChange() { |
|
this.setState(this._getLoginState()); |
|
} |
|
|
|
componentWillUnmount() { |
|
LoginStore.removeChangeListener(this.changeListener); |
|
} |
|
|
|
render() { |
|
return ( |
|
<ComposedComponent |
|
{...this.props} |
|
user={this.state.user} |
|
jwt={this.state.jwt} |
|
userLoggedIn={this.state.userLoggedIn} /> |
|
); |
|
} |
|
} |
|
};
|
|
|