Skip to content

Instantly share code, notes, and snippets.

@leandrojo
Created May 16, 2018 17:23
Show Gist options
  • Select an option

  • Save leandrojo/089e35814fe32156d02a89259609f799 to your computer and use it in GitHub Desktop.

Select an option

Save leandrojo/089e35814fe32156d02a89259609f799 to your computer and use it in GitHub Desktop.
import React from 'react';
import { Redirect, Route } from 'react-router-dom';
type Props = {
authed: boolean,
component: React.Component,
};
class PrivateRoute extends React.Component<void, Props, void> {
state = {};
render() {
const { component: Component, authed, ...rest } = this.props;
return (
<Route
{...rest}
render={props =>
(authed === true ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: '/login',
state: { from: props.location },
}}
/>
))
}
/>
);
}
}
PrivateRoute.defaultProps = {
location: {
state: {},
},
};
export default PrivateRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment