I have a component that the application goes through every time I change the screen. In this component I save isLogged: true
in the state, a variable that tells me if I have logged in.
When isLogged === true
, I redirected to the home screen, when isLogged === false
, I redirected to the signIn. If I log in, I enter my application and refresh the page (f5 or ctrl+r), lose the isLogged state and redirect me to signIn, because isLogged isn't true now.
I wanted to know if there is any way to save the isLogged variable, so that refreshing the page (being logged in) does not redirect me back to signIn. I have tried to save the state in local storage, but I see it unsafe. This is my code:
export class Layout extends Component {
constructor(props) {
super(props);
//definimos las variables necesarias
this.state = { isLogged: false, usuarioLogado: any };
//realizamos el bind de las funciones
this.DoLogin = this.DoLogin.bind(this);
}
//funcion donde establecemos los valores de las variables
DoLogin = (logged, usuario) =>
this.setState({
isLogged: logged,
usuarioLogado: usuario
});
render() {
if (this.state.isLogged) {
return <div>
<NavMenu headerTitle={this.props.headerTitle} textos={this.props.textos} valor={this.props.valor} changeLanguage={this.props.changeLanguage} usuario={this.state.usuarioLogado} labelData={this.props.labelData}/>
{this.props.children}
</div>
}
else return <SignIn callbackFromParent={this.DoLogin} labelData={this.props.labelData}/>;
}
}