Skip to content

Instantly share code, notes, and snippets.

@marcovega
Created August 24, 2020 03:02
Show Gist options
  • Save marcovega/1d64b5dedcd3718b44dfcb5067a01588 to your computer and use it in GitHub Desktop.
Save marcovega/1d64b5dedcd3718b44dfcb5067a01588 to your computer and use it in GitHub Desktop.
import * as React from 'react';
import { Redirect, Route, RouteProps } from 'react-router';
export const PrivateRoute: React.FC<RouteProps> = (props) => {
if (!localStorage.getItem('user')) {
const renderComponent = () => <Redirect to={{ pathname: '/login', state: { from: props.location } }} />;
return <Route {...props} component={renderComponent} render={undefined} />;
} else {
return <Route {...props} />;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment