3

Hi I am pretty new to ReactJS, I am trying to move to the login page when Logout is clicked, the function is getting executed but it wont redirect to the login page. I don't know how to do it. I have tried using this.props.history.push but that is also not working. I have also tried using Redirect to="/login" in the fakeAuth.signout function.

     import React, { Component } from 'react';
     import './App.css';
     import GetTodo from './component/getTodo.js';
     import AddTodo from './component/addTodo.js';
     import Login from './component/login.js';
     import {BrowserRouter as Router,Switch,Route,Link,Redirect} from 
     'react-router-dom';
     export const fakeAuth = {
       isAuthenticated: false,
       authenticate(cb) {
        this.isAuthenticated = true;
       },
       signout(cb) {
       this.isAuthenticated = false;
       }
      }
      const PrivateRoute = ({ component: Component, ...rest }) => (
      <Route {...rest} render={(props) => (
         fakeAuth.isAuthenticated === true
         ? <Component {...props} />
         : <Redirect to='/login' />
      )} />
     )
   class App extends Component {
     constructor(props){
      super(props);
       this.newsignout=this.newsignout.bind(this);
     }
    newsignout(){
      fakeAuth.signout();
      localStorage.clear();
      <Redirect to="/login" />
    }
    render() {
      return (
       <Router>
        <div className='App'>
                <nav className="navbar navbar-expand-lg navbar-light bg-light">               
                      <div className="collapse navbar-collapse">
                            <div className="navbar-nav">
                                  <div className="nav-item nav-link"><Link to={'/'}>GET TODO LIST</Link></div>
                                  <div className="nav-item nav-link"><Link to={'/add'}>ADD TODO</Link></div>
                                  <div className="nav-item nav-link"><Link to={'/login'}>LOGIN</Link></div>
                                  <div className="nav-item nav-link "><div className="LogOut" onClick={()=>this.newsignout()}>LOGOUT</div></div>
                            </div>
                      </div>
                </nav>
                <Switch>
                      <PrivateRoute exact path='/' component={GetTodo} />
                      <PrivateRoute path='/add/:id' component={AddTodo} />
                      <PrivateRoute exact path='/add' component={AddTodo} />
                      <Route path='/login' component={Login}/>
                </Switch>
        </div>  
    </Router>
   );
  }
}

export default App;
Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
kshitiz
  • 65
  • 6

2 Answers2

0

You can use "State" and chang this page without Redirect.

For example:

class MyComponent extends React.Component {
  constructor(){
    super();
    state = {
      isLogin: true
    }
  }

  newsignout(){
      this.setState({ isLogin: false });
    }

  render () {
    const { isLogin } = this.state;

     if (!isLogin) {
       return <LoginPage />; // for example
     }

     return <RenderYourForm/>; // other code
}
morteza ataiy
  • 541
  • 4
  • 12
0

this is working

       <div className="nav-item nav-link "><Link to={'/login'} className="LogOut" onClick={()=>this.newsignout()}>LOGOUT</Link></div>
kshitiz
  • 65
  • 6