Error:localhost redirected too many times

Whenever the user is authenticated and a web page is refreshed I keep getting this error "error:localhost redirected too many times try to clear cookies". Using codes below b
import { redirect } from 'next/navigation'
import { cookies } from 'next/headers'
import { verify,sign } from "jsonwebtoken";
import { COOKIE_NAME } from '../constants/cookie';
import prisma from "@/prisma/client";

export const revalidate = 0;

export default async function Protect({

children,
}: {
children: React.ReactNode
}){
const session = await getServerSession(authOptions)

// const user = await getUserAction()
// const cookieStore = cookies()
// const authorization = cookieStore.get('adminToken')
// console.log(authorization?.value)
// const user = JSON.parse(authorization!.value)

const cookieStore = cookies();

const token = cookieStore.get(COOKIE_NAME);

if (!token) {
redirect("/")
}

const { value } = token;

const secret = process.env.JWT_SECRET || "";

var decode = verify(value, secret) as { user: { id: string; email: string; name: string; image: string | null; role: string; hashedPassword: string }; iat: number; exp: number };
;

const useri = decode.user

const email = useri.email;


const user = await prisma.user.findFirst({
where:{
email:email,
ustate:"NON_BLOCKED"
},
select:{
id:true,
name:true,
role:true,
}

})

if (!user) {
redirect("/")
}

if(user.role == "USER"){
redirect("/home")
}

if(user.role == "ADMIN")
return(
<section>{children}</section>
)
}
import { redirect } from 'next/navigation'
import { cookies } from 'next/headers'
import { verify,sign } from "jsonwebtoken";
import { COOKIE_NAME } from '../constants/cookie';
import prisma from "@/prisma/client";

export const revalidate = 0;

export default async function Protect({

children,
}: {
children: React.ReactNode
}){
const session = await getServerSession(authOptions)

// const user = await getUserAction()
// const cookieStore = cookies()
// const authorization = cookieStore.get('adminToken')
// console.log(authorization?.value)
// const user = JSON.parse(authorization!.value)

const cookieStore = cookies();

const token = cookieStore.get(COOKIE_NAME);

if (!token) {
redirect("/")
}

const { value } = token;

const secret = process.env.JWT_SECRET || "";

var decode = verify(value, secret) as { user: { id: string; email: string; name: string; image: string | null; role: string; hashedPassword: string }; iat: number; exp: number };
;

const useri = decode.user

const email = useri.email;


const user = await prisma.user.findFirst({
where:{
email:email,
ustate:"NON_BLOCKED"
},
select:{
id:true,
name:true,
role:true,
}

})

if (!user) {
redirect("/")
}

if(user.role == "USER"){
redirect("/home")
}

if(user.role == "ADMIN")
return(
<section>{children}</section>
)
}
2 Replies
JulieCezar
JulieCezar9mo ago
I think this is because when this runs on the server you don't have any cookies at first, so you will always be redirected at the first if statement
Revaycolizer
Revaycolizer9mo ago
Thanks fixed it already