I have an application where people can access to the website if not login, however they should be able to access to "/app"
, "/app/*"
only if authenticated. My code below works but for some reason there is half a second, or a second of the content of "/app"
shown before showing the message "you're not allowed to..."
. Any idea why is that?
import { Provider, useDispatch, useSelector } from 'react-redux';
import { useRouter } from 'next/router';
import '../styles/front.css';
import '../styles/app.css';
import React, { useEffect, useState } from 'react';
import { wrapper, store } from '../store';
import { login, logout, selectUser } from "../redux/slices/userSlice";
import { auth } from '../firebase';
function MyApp({ Component, pageProps }) {
const user = useSelector(selectUser);
const dispatch = useDispatch();
const router = useRouter();
const isAppPage = router.pathname.startsWith('/app');
const [shouldRender, setShouldRender] = useState(true); // New state variable
const [loading, setLoading] = useState(true);
const [isAuthenticated, setIsAuthenticated] = useState(false);
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((userAuth) => {
if (userAuth) {
dispatch(login({
email: userAuth.email,
uid: userAuth.uid,
displayName: userAuth.displayName,
photoUrl: userAuth.photoURL
}));
setIsAuthenticated(true);
} else {
if (isAppPage) {
setShouldRender(false);
}
dispatch(logout());
setIsAuthenticated(false);
}
setLoading(false); // Set loading to false once the authentication status is checked
});
return () => unsubscribe(); // Cleanup the event listener when the component unmounts
}, []);
return (
<Provider store={store}>
{shouldRender ? <Component {...pageProps} /> : <p>You're not allowed to access that page.</p>}
</Provider>
);
}
export default wrapper.withRedux(MyApp);