Next.js Discord

Discord Forum

next build error cookies and location

Answered
Inca Dove posted this in #help-forum
Open in Discord
Inca DoveOP
i got error while building my next js app like the picture
its the cookie error because of nextauth and need to set the cookie first?
and none of my code are using location, all of them using useRouter from next/navigation

how to fix this?
Answered by Ray
ok try change this
    if (session?.role !== "ADMIN") {
        router.push("/dashboard");
    }

to
  useEffect(() => {
    if (session?.role !== "ADMIN") {
    router.push("/dashboard");
    }
  }, [session])
View full answer

9 Replies

Inca DoveOP
ok try change this
    if (session?.role !== "ADMIN") {
        router.push("/dashboard");
    }

to
  useEffect(() => {
    if (session?.role !== "ADMIN") {
    router.push("/dashboard");
    }
  }, [session])
Answer
Inca DoveOP
let me try
wow its works
so the problem is the cookie from the nextauth right?

but anyway, thanks a lot!
it try to use router.push() on the server because client component will still render on the server
@Inca Dove so the problem is the cookie from the nextauth right? but anyway, thanks a lot!
np, please mark solution if you problem has been solved