Skip to content

Instantly share code, notes, and snippets.

@hckhanh
Created May 20, 2019 04:13
Show Gist options
  • Save hckhanh/5570f7c7383f0f513b2df4e1c036d3da to your computer and use it in GitHub Desktop.
Save hckhanh/5570f7c7383f0f513b2df4e1c036d3da to your computer and use it in GitHub Desktop.
import React from 'react';
import { Redirect, Route } from 'react-router-dom';
import { checkAuthentication } from '../../store/selectors/user';
function withAuthRoute(checkAuth, pathname) {
return function AuthRoute({ component: Component, ...rest }) {
return (
<Route
{...rest}
render={props => {
if (checkAuth()) {
return <Component {...props} />;
}
return (
<Redirect to={{ pathname, state: { from: props.location } }} />
);
}}
/>
);
};
}
export const PrivateRoute = withAuthRoute(
() => checkAuthentication(),
'/login'
);
export const PublicOnlyRoute = withAuthRoute(
() => !checkAuthentication(),
'/workplace/select'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment