Skip to content

Instantly share code, notes, and snippets.

@ooade
Created July 21, 2016 22:30
Show Gist options
  • Save ooade/fc6cc0ea800fc676b08c1a159e673494 to your computer and use it in GitHub Desktop.
Save ooade/fc6cc0ea800fc676b08c1a159e673494 to your computer and use it in GitHub Desktop.
Main HOC
import React, { Component } from 'react';
// Grab the {createContainer} from react-meteor-data pckg and react-router pckg
import { createContainer } from 'meteor/react-meteor-data';
import { browserHistory } from 'react-router';
// Wrap our component in a function with the ComposedComponent as an argument
// ComposedComponent is the component to be rendered
export default function(ComposedComponent) {
class RequireAuth extends Component {
componentWillMount() {
if (!this.props.authenticated) {
browserHistory.push('/signin');
}
}
componentWillUpdate(nextprops) {
if (!nextprops.authenticated) {
browserHistory.push('/signin');
}
}
render() {
return (
// {...this.props} ensures that each components props are not thrown away :p
<ComposedComponent {...this.props} />
);
}
};
return createContainer(() => {
return { authenticated: !!localStorage.getItem('auth') }
}, RequireAuth);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment