Using react + react-router-dom:
import React from 'react';
import { Switch, Route, Redirect } from 'react-router-dom'; 
And protecting a route by this:
Router
const Router = () => {
    return (
        <Switch>
            <PrivateRoute exact path='/Panel' component={Panel}></PrivateRoute>
            <Route exact path='/Register' component={Register}></Route>
            <Route exact path='/Login' component={Login}></Route>
        </Switch>
    );
};
const PrivateRoute = ({ component: Component, ...rest }) => (
    <Route
        {...rest}
        render={props =>
            Auth.getAuth() ? (
                <Component {...props} />
            ) : (
                <Redirect
                    to={{
                        pathname: "/Login"
                    }}
                />
            )
        }
    />
);
Auth
const Auth = {
    isAuthenticated: false,
    authenticate() {
        this.isAuthenticated = true;
    },
    signout() {
        this.isAuthenticated = false;
    },
    getAuth() {
        return this.isAuthenticated;
    }
};
So this working fine and user easily can login, logout, but the problem is when user logged I want to redirect user to /Panel route so I tried:
window.location.href = "/Panel"
OR:
this.props.history.push('/Panel')
Both redirect to /Login again too fast, but If I click on Panel link it going to Panel route. Second problem is when I refresh page on this address /Panel it bring me back to /Login again. So what I want to solve are:
- How to redirect to protected route?
- How to avoid redirect when refresh page on protected route? If I type this address and enter it not working too.
Already seen this topic and tried but no success:
 
    