0

I have used a state variable in redux and change it after login. how can i use that to change if the user i logged in or not?

When i refresh the page, it redirects to the login page.

        if(authError === "") return <Redirect to='/login/' />
        return (
            <div>
                <Header />
                <Router history={history}>
                    <Switch>
                        <Route exact path="/" component={Home} />
                        <Route path="/contactus" component={ContactUs} />
                        <Route path="/jobsnearyou" component={JobsNearYou} />
                        <Route path="/CandidateChat" component={CandidateChat} />
                        <Route path="/myprofile" component={MyProfile} />
                        <Route path="/candidateprofile" component={CandidateProfile} />
                        <Route path="/employerprofile" component={EmployerProfile} />

                    </Switch>
                </Router>
                <Footer />
            </div>
        );

i change "authError=null" if logged in

Rakesh Rajan
  • 75
  • 1
  • 2
  • 5
  • Possible duplicate of [How to implement authenticated routes in React Router 4?](https://stackoverflow.com/questions/43164554/how-to-implement-authenticated-routes-in-react-router-4) – Harald Gliebe Mar 30 '19 at 10:40

1 Answers1

0

It's the expected result. Once you reload the page you'll always get the initial value you set in the reducer. Then, if you have the "" value as default, it should always redirect to the login page upon reloading. One way of solving that problem would be saving the login state in the local storage, and retrieving that value during the page mounting.

You can check how the local storage works here:

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage