I’m going to do my best to explain my problem here, but it’s going to be challenging so bear with me.
I have a web app developed in react + firebase with firebase authentication, the login is for now only handled with the mail and password provider. I’m using react router and firebase 9.
When someone logs in or out the auth token is persisted in session storage, and I have a condition in the App component (the first layer on my page) that shows a log in/register link if the user isn’t logged in and a my account link if the user is logged in. The problem is that I need to reload the page to see the changes, When logging in you get redirected to another route, but being a react app it doesn’t reload the whole page.
My App.js
const auth = getAuth();
const App = () => {
const [user, setUser] = useState();
const [expanded, setExpanded] = React.useState(true);
const [activeKey, setActiveKey] = React.useState('1');
const handleLogout = () => {
signOut(auth).then(()=> {
setUser({});
sessionStorage.clear();
console.log("unset user");
}).catch((err) => {
console.error(err);
alert(err.message);
})
};
onAuthStateChanged(auth, (user) => {
if (user) {
setUser(user)
sessionStorage.setItem("user", JSON.stringify(user))
const uid = user.uid;
console.log("set user")
} else {
}
});
useEffect(() => {
const loggedInUser = sessionStorage.getItem("user");
if (loggedInUser != null) {
const foundUser = loggedInUser.toString();
setUser(foundUser);
console.log(user)
}
}, []);
return (
<div>
<li className="nav-item">
{
(sessionStorage.user ?
<div className="d-flex justify-content-around row">
<Link className="nav-link" to="/user/perfil">Mi cuenta</Link>
</div>
: <Link className="nav-link" to="/login">Iniciar sesión / Registrarse</Link>
)
}
</li>
</div>
)
}
export default App;
My login.js
const auth = getAuth();
console.log(auth.currentUser);
const loginWithEmailAndPassword = async (email, password) => {
setPersistence(auth, browserSessionPersistence)
.then(() => {
signInWithEmailAndPassword(auth, email, password)
}).catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(error.message)
})
}
class Login extends Component {
constructor() {
super();
this.state = {
email: "",
password: "",
redirect: "",
};
}
handleChangeEmail = () => {
this.setState({ email: document.getElementById("email").value });
console.log(this.state);
}
handleChangePassword = () => {
this.setState({ password: document.getElementById("password").value })
}
handleRedirect = () => {
this.setState({ redirect: "/aplicacion" });
console.log(this.state);
}
handleClick = async () => {
try {
await loginWithEmailAndPassword(
this.state.email,
this.state.password
);
this.handleRedirect();
} catch (err) {
console.error(err);
alert(err.message);
}
};
render() {
if (this.state.redirect) {
return <Navigate to={this.state.redirect} />
}
return (
<div className="d-flex justify-content-center" >
<div className="col-6">
<Link to='/recuperacion' className="tiny-text">He olvidado mi contraseña</Link>
</div>
</div>
)
}
}