Skip to content

Instantly share code, notes, and snippets.

@gkueny
Last active January 30, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkueny/8f754d50c1c100e0f8abb315e5cdab26 to your computer and use it in GitHub Desktop.
Save gkueny/8f754d50c1c100e0f8abb315e5cdab26 to your computer and use it in GitHub Desktop.
react admin article : https://gkueny.fr/react-admin
import React, {Component} from 'react';
import * as firebase from 'firebase';
class HaveToLogginComponent extends Component {
constructor() {
super();
this.state = {
authenticate : false
}
}
componentDidMount() {
this.checkUserStatus();
}
componentDidUpdate() {
this.checkUserStatus();
}
checkUserStatus = () => {
firebase.auth().onAuthStateChanged( user => {
let statut = user
? true
: false;
this.updateUserStatus(statut);
});
};
updateUserStatus = (isLogIn) => {
// User not login, and previously authenticate
if (!isLogIn && this.state.authenticate)
this.setState({
authenticate : false
});
// User login, and previously not authenticate
else if (isLogIn && !this.state.authenticate)
this.setState({
authenticate : true
});
}
render() {
if (this.props.state.user.userLogin)
return this.props.children;
else
return <p> error </p>;
}
}
HaveToLogginComponent.propTypes = {
children : React.PropTypes.object
};
export default HaveToLogginComponent;
//react admin
import React, {Component} from 'react';
export default class AdminComponent extends Component
{
constructor() {
this.isAuthenticate = false;
}
componentDidMount() {
this.checkLogin();
}
componentDidUpdate() {
this.checkLogin();
}
checkLogin() {
this.isAuthenticate = false;
if(this.props.functionOfContainerWhichCheckLogin())
this.isAuthenticate = true;
else
this.props.functionWhichGoToLogin();
}
render() {
if(!this.isAuthenticate) {
return null;
} else {
return (
<p> Admin </p>
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment