#NEXT_REDIRECT on middleware

8 messages · Page 1 of 1 (latest)

paper badger
#

Hello everyone, I have the following code:

export function middleware(request: NextRequest) {
    console.log('middleware run')
    const cookies = request.cookies.getAll()
    if(!cookies.length){
        redirect("/auth/login")
    }
    // return NextResponse.next()
}

As it says in the title, I'm getting a "NEXT_REDIRECT" error on my middleware when the user is not authenticated.

I would expect to get redirected to the URL I specificied instead.

Any ideas why this is happening ?

dim chasmBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

verbal cradle
#

redirect is not meant to be called in the middleware. Use NextResponse.redirect instead.

#

The docs have examples and notes on that specifically

paper badger
verbal cradle
#

That page doesn't mention the middleware for one. Also you should read the actual middleware docs.

paper badger
#

Copy that, will do

#

Thanks for the help!