Skip to content

Instantly share code, notes, and snippets.

@ryanbelke
Created October 12, 2018 02:37
Show Gist options
  • Select an option

  • Save ryanbelke/a04a1433418a162d5a54c63b8ad3b8cb to your computer and use it in GitHub Desktop.

Select an option

Save ryanbelke/a04a1433418a162d5a54c63b8ad3b8cb to your computer and use it in GitHub Desktop.
/* components/hocs/securePage.js */
import React from "react";
import PropTypes from "prop-types";
import defaultPage from "./defaultPage";
const securePageHoc = Page =>
class SecurePage extends React.Component {
static propTypes = {
isAuthenticated: PropTypes.bool.isRequired
};
static getInitialProps(ctx) {
return Page.getInitialProps && Page.getInitialProps(ctx);
}
render() {
const { isAuthenticated } = this.props;
return isAuthenticated ? <Page {...this.props} /> : "Not Authorized";
}
};
export default Page => defaultPage(securePageHoc(Page));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment