Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to set cookies in Vercel edge-middleware when using withMiddlewareAuthRequired() #1253

Closed
6 tasks done
byzyk opened this issue Jun 15, 2023 · 1 comment · Fixed by #1263
Closed
6 tasks done
Labels
bug Something isn't working

Comments

@byzyk
Copy link

byzyk commented Jun 15, 2023

Checklist

Description

Following the middleware examples - setting up a custom cookie works locally but has no effect when deployed to Vercel Edge.

Removing withMiddlewareAuthRequired and replicating its functionality fixes the issue - cookies are added as expected.

Reproduction

  1. middleware.js
export default withMiddlewareAuthRequired(async function middleware(req) {
  const res = NextResponse.next();

  const session = await getSession(req, res);

  if (session?.user) {
    res.cookies.set('name', 'value');
  }

  return res;
});

export const config = {
  runtime: 'experimental-edge',
  matcher: [
    '/',
  ],
};
  1. Deploy to Vercel Edge -> doesn't work
  2. Remove withMiddlewareAuthRequired
  3. Deploy to Vercel Edge -> works!

Additional context

Something similar is described here: #1045 (comment)

nextjs-auth0 version

2.6.2

Next.js version

13.4.5

Node.js version

18.16.0

@adamjmcgrath
Copy link
Contributor

Thanks for raising this @byzyk

Something similar is described here: #1045 (comment)

Yep - it's the same issue as the one you pointed to, I need to apply this fix to another part of the code.

As a workaround, you can do something like this for the time being

export default async function middleware(req) {
  const res = await withMiddlewareAuthRequired()(req);

  const session = await getSession(req, res);

  if (session?.user) {
    res.cookies.set('name', 'value');
  }

  return res;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants