Skip to content

fix(ts): handle NextRequest type #4472

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

Merged
merged 4 commits into from
Apr 28, 2022
Merged

fix(ts): handle NextRequest type #4472

merged 4 commits into from
Apr 28, 2022

Conversation

0ubbe
Copy link
Collaborator

@0ubbe 0ubbe commented Apr 26, 2022

💡 Reasoning

Following the merge of this PR #4402, the build got broken 🔥

That's because the getToken() implementation assumed the req object passed to it was an instance of NextApiRequest. However, the new middleware implementation:

https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/next/middleware.ts#L56-L84

is calling this function with a req object which is an instance of NextRequest.

At the end of the day:

const token = getToken({ req })

is only interested in cookies and headers in the req object supplied to it. It happens though that headers are different in NextApiRequest and NextRequest... in NextApiRequest it'll be IncomingHttpHeaders while in NextRequest it'll be Headers and both are read differently...

Random thought

Was getToken() working before when not called in the context of an API route?

// my-page.tsx
export async function getServerSideProps(context) {
  const token = getToken({ req: context.req })
  // ...

🧢 Checklist

  • [ ] Documentation
  • [ ] Tests
  • Ready to be merged

🎟 Affected issues

None

Sorry, something went wrong.

@vercel
Copy link

vercel bot commented Apr 26, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
next-auth ⬜️ Ignored (Inspect) Apr 26, 2022 at 0:16AM (UTC)

@github-actions github-actions bot added the core Refers to `@auth/core` label Apr 26, 2022
Copy link
Member

@balazsorban44 balazsorban44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@balazsorban44 balazsorban44 changed the title fix(jwt): handle NextRequest type fix(ts): handle NextRequest type Apr 26, 2022
@github-actions github-actions bot added the client Client related code label Apr 26, 2022
@balazsorban44 balazsorban44 merged commit fb4bbc3 into main Apr 28, 2022
@balazsorban44 balazsorban44 deleted the fix/types branch April 28, 2022 09:22
mnphpexpert added a commit to mnphpexpert/next-auth that referenced this pull request Sep 2, 2024
* fix(jwt): handle NextRequest

* fix(cookie): Headers from fetch request

* Update packages/next-auth/src/jwt/index.ts

* fix: tests

Co-authored-by: Balázs Orbán <info@balazsorban.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client Client related code core Refers to `@auth/core`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants