Skip to content

Instantly share code, notes, and snippets.

@VictorCoding
Created October 8, 2016 02:01
Show Gist options
  • Save VictorCoding/dd3996a4f29aad694af45f0924f4e349 to your computer and use it in GitHub Desktop.
Save VictorCoding/dd3996a4f29aad694af45f0924f4e349 to your computer and use it in GitHub Desktop.
auth comonent
class MatchWhenAuthorized extends Component {
constructor(props) {
super();
console.log(props);
this.props = props;
this.state = {
isAuthenticated: false,
}
}
componentWillMount() {
Firebase.auth().onAuthStateChanged((user) => {
this.setState({
isAuthenticated: user ? true : false,
});
});
}
render() {
const { isAuthenticated } = this.state;
const props = this.props;
return (
<Match {...props} render={props => (
isAuthenticated ? (
<this.props.component {...this.props}/>
) : (
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}}/>
)
)}/>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment