I'm trying to protect two different routes that to access that routes user must need to be authenticated I also followed this question but it doesn't help me at all. In my Header.js all the logic has been given on that file to check whether the user is logged in or not ( using redux action, reducer ) So I'm directly giving <Redirect to='/surveys'/>
this redirect link will be accessed when the user logged in kind of like welcome page. And there is another route /surveys/new
whenever going in this route if in some condition browser gets refreshed I'm thrown again to the /surveys
link I want to stay that page whenever the user reload the browser. Plz check the renderLocation()
function...I'm totally fed up trying to find the solution plz help me somebody
here is my Header.js file
import React, { Component } from 'react';
import { connect } from "react-redux";
import { Link, Redirect} from 'react-router-dom';
import Payments from "./Payments";
class Header extends Component {
// renderLocation(){
// if(window.location.reload(window.location.pathname === '/surveys')){
// return <Redirect key='6' to='/surveys' />;
// }else if(window.location.reload(window.location.pathname === '/surveys/new')){
// return <Redirect key='7' to='/surveys' />;
// }else{
// return <Redirect key='8' to='/surveys' />;
// }
// }
renderContent(){
switch(this.props.auth){
case null:
return;
case false:
return [
<li key="4"><a href="/auth/google" className="btn"><strong>Login With Google</strong></a></li>,
<Redirect key="5" to="/" />
];
default:
return [
<li key="1"><Payments /></li>,
<li key="2"><button className = "btn ml-3"><strong>CREDITS : {this.props.auth.credits}</strong></button></li>,
<li key="3"><a href="/api/logout" className="btn"><strong>Logout</strong></a></li>,
// <div>{() => this.renderLocation()}</div>,
<Redirect key="8" to='/surveys' />,
]
}
}
render() {
return (
<nav style={{marginBottom:"20px"}}>
<div className="nav-wrapper">
<div className="container">
<Link to={this.props.auth ? '/surveys' : '/'}
className="left brand-logo" style={{textDecoration : "none", fontFamily: "'Pacifico', cursive"}}> Emaily
</Link>
<ul id="nav-mobile" className="right">
{this.renderContent()}
</ul>
</div>
</div>
</nav>
)
}
}
const mapStateToProps = (state) => {
return { auth : state.auth}
}
export default connect(mapStateToProps)(Header);