I am trying to secure all of my routes in react for that I am trying one logic but it is not working the way I want it to be.
I am using the app.js file in that I have my routes file. I am using a functional component.
My code in app.js is:
export default function App(props) {
useEffect(() => {
}, []);
var logged = null
var user = firebase.auth().currentUser;
if (user) {
logged = true
console.log("user exist", user);
} else {
logged = false
console.log("user don't exist", logged);
}
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props =>
//fakeAuth.isAuthenticated === true ? (
logged === true ? (
<Component {...props} />
) : (
<Redirect to="/signIn" />
)
}
/>
);
<PrivateRoute path="/features" component={FeaturePage} />
}
This code is working fine but when I am refreshing the page or trying to go to the URL from browser it is not working.
When I am trying to add the logic in:
var logged = null
useEffect(() => {
// CLEAN UP
var user = firebase.auth().currentUser;
if (user) {
logged = true
console.log("user exist", user);
} else {
logged = false
console.log("user don't exist", logged);
}
return () => {
document.querySelector('.first-row').style.position = '';
};
}, []);
It doesn't work my question is how to create my app.js file check if the user is logged in or not every time if the user opens route from the browser or any way. It should know if it is logged in or not.
Firebase please provide some examples or any answer will be appreciated.