Skip to content

Instantly share code, notes, and snippets.

@codeAdrian
Last active August 23, 2019 07:54
Show Gist options
  • Save codeAdrian/51582f270bf867f95e9e9c23871a6442 to your computer and use it in GitHub Desktop.
Save codeAdrian/51582f270bf867f95e9e9c23871a6442 to your computer and use it in GitHub Desktop.
React Protected Route Component - based on https://tylermcginnis.com/react-router-protected-routes-authentication/ and extended with redirect prop
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
const PrivateRoute = ({
component: Component,
isAuthenticated,
redirectTo,
...rest
}) => (
<Route
{...rest}
render={props =>
isAuthenticated ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: redirectTo ? redirectTo : '/login',
state: { from: props.location }
}}
/>
)
}
/>
);
export default PrivateRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment