Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
Add Comment
Please, Sign In to add comment