[Solved] PrivateRouting when Token in Local Storage [TypeScript]


This will go in your index.tsx file:

const token = localStorage.getItem('token');

const PrivateRoute = ({component, isAuthenticated, ...rest}: any) => {
    const routeComponent = (props: any) => (
        isAuthenticated
            ? React.createElement(component, props)
            : <Redirect to={{pathname: '/login'}}/>
    );
    return <Route {...rest} render={routeComponent}/>;
};

And use this in the browser router/switch:

<PrivateRoute
    path="/panel"
    isAuthenticated={token}
    component={PrivateContainer}
/>

solved PrivateRouting when Token in Local Storage [TypeScript]