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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Next.js Version 13.0.1 #5701

Closed
HaNdTriX opened this issue Nov 1, 2022 · 3 comments 路 Fixed by #5710
Closed

Support Next.js Version 13.0.1 #5701

HaNdTriX opened this issue Nov 1, 2022 · 3 comments 路 Fixed by #5710
Labels
enhancement New feature or request triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@HaNdTriX
Copy link
Contributor

HaNdTriX commented Nov 1, 2022

Description 馃摀

Next.js 13.0.1 introduces the following breaking change: vercel/next.js#41526

TLTR: Before request.cookies.get(<name>) returned the cookie value. Now this invocation returns the following object:

{ name: <name>, value: <value>}

resulting in jwt.getToken to always return null

How to reproduce 鈽曪笍

Node.js Example (Works)

import type { NextApiRequest, NextApiResponse } from "next";
import { getToken } from "next-auth/jwt";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
  res.status(200).json({ token });
}

This code returns the correct token.

Edge Example (Doesn't work)

import type { NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";

export const config = {
  runtime: "experimental-edge",
};

export default async function handler(req: NextRequest) {
  const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
  return new Response(
    JSON.stringify({
      token,
    }),
    {
      status: 200,
      headers: {
        "content-type": "application/json",
      },
    }
  );
}

This code always returns token: null.

System Info

  • Used Next.js Version: 13.0.1

Related Code

if (cookies instanceof Map) {
for (const name of cookies.keys()) {
if (name.startsWith(cookieName)) this.#chunks[name] = cookies.get(name)
}
} else {
for (const name in cookies) {
if (name.startsWith(cookieName)) this.#chunks[name] = cookies[name]
}
}

Contributing 馃檶馃徑

Yes, I am willing to help implement this feature in a PR

@HaNdTriX HaNdTriX added enhancement New feature or request triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels Nov 1, 2022
@goliney
Copy link

goliney commented Nov 1, 2022

I've just encountered the same issue. I upgraded next to 13.0.1 and faced the issue in middleware.ts:

import { getToken } from 'next-auth/jwt';

export default async function middleware(req) {
  const token = await getToken({ req });
  console.log(token); // always null
}

Downgraded to next@13.0.0 and the issue is gone.

@sandervspl
Copy link

sandervspl commented Nov 1, 2022

This patch will fix it for now https://gist.github.com/sandervspl/269e3900b7e4de6ff6f480e476942272

@jack-szeto
Copy link

I've just encountered the same issue. I upgraded next to 13.0.1 and faced the issue in middleware.ts:

import { getToken } from 'next-auth/jwt';

export default async function middleware(req) {
  const token = await getToken({ req });
  console.log(token); // always null
}

Downgraded to next@13.0.0 and the issue is gone.

@goliney you saved my day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants