'How can i set auto redirect if cookie was set before in nextjs?
Halo, i'm very confuse with this condition.. i already set cookies if user was regist or login before.. but if i refresh the page.. the cookie was not read and not directed to page that i set before
my code is
import "bootstrap/dist/css/bootstrap.min.css";
import { useStore } from "../src/redux/store";
import { Provider } from "react-redux";
import { useEffect } from "react";
import { authService } from "../src/services/AuthServices";
import { useRouter } from "next/router";
import { SSRProvider } from "react-bootstrap";
function MyApp({ Component, pageProps }) {
const store = useStore(pageProps.initialReduxState);
const dataAuth = authService.isLogin();
const router = useRouter();
const routeList = ["/login"];
useEffect(() => {
if (!dataAuth) {
router.push("/");
}
}, []);
return (
<SSRProvider>
<Provider store={store}>
<Component {...pageProps} />
</Provider>
</SSRProvider>
);
}
export default MyApp;
does anyone can help ?
Solution 1:[1]
you should include dataAuth in the useEffect dependencies array, see the code below:
useEffect(() => {
if (!dataAuth) {
router.push("/");
}
}, [dataAuth]); <-it will make this effect run every time the dataAuth changes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mocherfaoui |
