Skip to content

Instantly share code, notes, and snippets.

@hieptl
Created October 20, 2021 15:47
Show Gist options
  • Save hieptl/7dbabd7a3f71faed65187ef4b12dc544 to your computer and use it in GitHub Desktop.
Save hieptl/7dbabd7a3f71faed65187ef4b12dc544 to your computer and use it in GitHub Desktop.
PrivateRoute.tsx - Ionic Chat App
// import react.
import React from 'react';
// import react router.
import { Route, Redirect } from 'react-router-dom';
const PrivateRoute: React.FC<any> = ({ component: Component, ...rest }: any) => {
return (
// Show the component only when the user is logged in
// Otherwise, redirect the user to /signin page
<Route {...rest} render={props => (
localStorage.getItem('auth') ?
<Component {...props} />
: <Redirect to="/login" />
)} />
);
};
export default PrivateRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment