Skip to content

Instantly share code, notes, and snippets.

@AZagatti
Created July 28, 2020 16:46
Show Gist options
  • Save AZagatti/35337c7953493f3f31369b507747227c to your computer and use it in GitHub Desktop.
Save AZagatti/35337c7953493f3f31369b507747227c to your computer and use it in GitHub Desktop.
import React from 'react';
import {
Route as ReactDOMRoute,
RouteProps as ReactDOMRouteProps,
Redirect,
} from 'react-router-dom';
import { useAuth } from '../hooks/auth';
interface RouteProps extends ReactDOMRouteProps {
isPrivate?: boolean;
component: React.ComponentType;
}
const Route: React.FC<RouteProps> = ({
isPrivate = false,
component: Component,
...rest
}) => {
const { user } = useAuth();
// You can store user data in another way and only retrieve it here
return (
<ReactDOMRoute
{...rest}
render={({ location }) => {
return isPrivate === !!user ? (
<Component />
) : (
<Redirect
to={{
pathname: isPrivate ? '/' : '/dashboard',
state: { from: location },
}}
/>
);
}}
/>
);
};
export default Route;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment