10

I'm attempting to create some route guarding using the new Next.Js 12 middleware feature. My authentication is based on a JWT token set on a cookie. I had previously implemented this using the API backend on Next.Js with no issues, and still when hitting the API routes the cookie will persist on the request no problem.

My issue appears when it will request a static page from the server. No cookies are attached so I can not determine if a User is authenticated and always redirect to a log in page. So for example the request to http://localhost:3000/ (Homepage) will not send any cookies to the middleware. But, http://localhost:3000/api/user will send a cookie to the middleware. Is there a setting I have missed in the documentation to allow this to happen?

Not sure if at all helpful but here is my _middleware.ts file that sits on the root of the pages.

import type { NextFetchEvent, NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

const middleware = (req: NextRequest, ev: NextFetchEvent) => {
  console.log(req.cookies);
  console.log(req.cookies['user']);
  console.log(req.nextUrl.pathname);
  if (req.nextUrl.pathname === '/') {
    return NextResponse.redirect('http://localhost:3000/login');
  }
};

export default middleware;
5
  • can you share your _middleware.js code Commented Mar 12, 2022 at 23:45
  • @Yilmaz Not sure how helpful it is at the moment, kind of just debugging code but i've included it in the main question body now. Commented Mar 13, 2022 at 1:14
  • So the request to the homepage / will have cookie user undefined, but the request to /api/user will have a cookie attached to it. Commented Mar 13, 2022 at 1:14
  • 2
    Hey did you ever find a solution to this? Having the same problem when redirecting to my Next app from Stripe Checkout. Commented Aug 18, 2022 at 12:47
  • @Ronald Blüthl- did you solve the problem? Commented Mar 11, 2023 at 9:20

3 Answers 3

6

Next.js > 12.2

req.cookies.get("user")

Before Next.js 12.2,

req.cookies?.user
Sign up to request clarification or add additional context in comments.

5 Comments

@ARSecond do u confirm that you set up the cookie correctly on the browser
Yes, This is a simple project with 4 pages. you can see the problem (this happens in the production not development.) github.com/darushHamidi/next-middleware
do you have envvriables in your project. maybe u did not set correctly for production
this is a small sample project and I did not use any env variables. if you see this small repo you will notice the problem. github.com/darushHamidi/next-middleware
0

I had a same problem. I use tag for routing and it works but im not sure that it is a good choice.

Comments

0

To check if the cookie exists

request.cookies.has('YOUR_COOKIE_NAME')

To get the cookie value

request.cookies.get('YOUR_COOKIE_NAME')?.value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.