Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created February 15, 2019 12:16
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/da02cc8fad7a149eac56a3d246973ed1 to your computer and use it in GitHub Desktop.
Save sibelius/da02cc8fad7a149eac56a3d246973ed1 to your computer and use it in GitHub Desktop.
Auth middleware to use with RR4
import * as React from 'react';
import { withRouter } from 'react-router-dom';
import { isLoggedIn } from '../../security/authentication';
import routeTo from '../utils/routeTo';
const authenticatedMiddleware = Component => {
class AuthenticatedMiddleware extends React.PureComponent {
componentDidMount() {
this.redirectIfNotAuthenticated();
}
componentDidUpdate() {
this.redirectIfNotAuthenticated();
}
redirectIfNotAuthenticated = async () => {
if (!isLoggedIn()) {
logout();
this.props.history.push(routeTo('auth.login'));
}
};
render() {
if (!isLoggedIn()) {
return null;
}
return <Component {...this.props} />;
}
}
return AuthenticatedMiddleware;
};
export default authenticatedMiddleware;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment