Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
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 ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!