Created
February 15, 2019 12:16
-
-
Save sibelius/da02cc8fad7a149eac56a3d246973ed1 to your computer and use it in GitHub Desktop.
Auth middleware to use with RR4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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