I have a component where need user authentication, if the user not logged in, there will be a login form displayed. Otherwise display the content.
Here is what I did:
The main layout for the component.
export default class MainLayout extends TrackerReact(React.Component) {
constructor(props) {
super(props);
this.state = {
isLoggedIn : User.isLoggedIn()
};
}
logout(e){
Meteor.logout( (err) =>{
if(err){
Bert.alert(error['reason'], 'danger', 'fixed-top', 'fa-frown-o' );
}else{
this.setState({isLoggedIn: !User.isLoggedIn()});
}
});
}
render() {
if(User.isLoggedIn()){
return (
<div>
<SiteHeader logout={this.logout} menu='LoginMenu'/>
<div className="container-fluid top-offset">
{this.props.content}
</div>
</div>)
}else{
return (<Login />)
}
}
}
And this is the SiteHeader component:
export default class SiteHeader extends React.Component {
logout(e) {
e.preventDefault();
this.props.logout(e)
}
render(){
if(this.props.menu == 'LoginMenu'){
menu =(
<ul className="nav navbar-nav navbar-right">
<li><a href="/" >Home</a></li>
<li><a href="/dashboard">My Project</a></li>
<li><a href="#" onClick={this.logout.bind(this)}>Logout</a></li>
</ul>
);
}else{
menu =(
<ul className="nav navbar-nav navbar-right">
<li><a href="/" >Home</a></li>
<li><a href="/login">Login</a></li>
</ul>
);
}
return ( {menu} )
}
}
SiteHeader.propTypes = {
logout : React.PropTypes.func
};
So when I logged-in, I can see the dom updated to the authorized content , but when I logout, the dom is not updating to a login form, however the user is logged out properly.
When I click on the logout link, this is what i get from the console:
xception in delivering result of invoking ‘logout’: TypeError: _this2.setState is not a function
at http://localhost:3000/app/app.js?hash=c420dcaf4d58b9f867c19634b52cae3fae730321:510:13
at http://localhost:3000/packages/accounts-base.js?hash=9a2df45ebeba9d14f693547bc91555a09feda78e:201:23
at null._callback (http://localhost:3000/packages/meteor.js?hash=ae8b8affa9680bf9720bd8f7fa112f13a62f71c3:1105:22)
at _.extend._maybeInvokeCallback (http://localhost:3000/packages/ddp-client.js?hash=b5f1b97df6634673c68f37914ae9f4c3231c438e:3541:12)
at _.extend.receiveResult (http://localhost:3000/packages/ddp-client.js?hash=b5f1b97df6634673c68f37914ae9f4c3231c438e:3561:10)
at .extend.livedata_result (http://localhost:3000/packages/ddp-client.js?hash=b5f1b97df6634673c68f37914ae9f4c3231c438e:4681:9)
at onMessage (http://localhost:3000/packages/ddp-client.js?hash=b5f1b97df6634673c68f37914ae9f4c3231c438e:3369:12)
at http://localhost:3000/packages/ddp-client.js?hash=b5f1b97df6634673c68f37914ae9f4c3231c438e:2736:11
at Array.forEach (native)
at Function..each..forEach (http://localhost:3000/packages/underscore.js?hash=27b3d669b418de8577518760446467e6ff429b1e:149:11)
debug.js:41 Exception from Tracker recompute function:
debug.js:41 TypeError: Cannot read property ‘call’ of undefined
at Dashborad.TrackerReactComponent._this.constructor.componentWillUnmount (main.js:47)
at ReactCompositeComponentMixin.unmountComponent (ReactCompositeComponent.js:316)
at Object.ReactReconciler.unmountComponent (ReactReconciler.js:65)
at Object.ReactChildReconciler.unmountChildren (ReactChildReconciler.js:117)
at ReactDOMComponent.ReactMultiChild.Mixin.unmountChildren (ReactMultiChild.js:325)
at ReactDOMComponent.Mixin.unmountComponent (ReactDOMComponent.js:883)
at Object.ReactReconciler.unmountComponent (ReactReconciler.js:65)
at Object.ReactChildReconciler.unmountChildren (ReactChildReconciler.js:117)
at ReactDOMComponent.ReactMultiChild.Mixin.unmountChildren (ReactMultiChild.js:325)
at ReactDOMComponent.Mixin.unmountComponent (ReactDOMComponent.js:883)
Is this the meteor that causing this problem or it just the react js?