question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting token in middleware doesn't work with authorization header request

See original GitHub issue

Environment

System: OS: macOS 11.6 CPU: (4) x64 Intel® Core™ i5-7360U CPU @ 2.30GHz Memory: 169.62 MB / 16.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 16.13.0 - /usr/local/bin/node Yarn: 1.22.15 - ~/.yarn/bin/yarn npm: 8.1.0 - /usr/local/bin/npm Browsers: Brave Browser: 72.0.59.35 Chrome: 98.0.4758.102 Edge: 98.0.1108.56 Safari: 14.1.2 npmPackages: next: ^12.0.7 => 12.0.9 next-auth: ^4.2.1 => 4.2.1 react: 17.0.2 => 17.0.2

Reproduction URL

None

Describe the issue

When using just the authorization header, the getToken(req) method returns correctly in the server with NextApiRequest, but not in the middleware with NextRequest. When I dug through the code, it seems it’s because NextApiRequest authorization header is in the list, whereas getToken function is trying to split the authorization header by space, which shouldn’t work for this case.

How to reproduce

For any OAuth provider, get the raw token.

Use it from a REST client with the Authorization: Bearer <token> and send an API request.

In the middleware, try getToken({req}). It returns null, whereas in the actual API route code, it returns the token info correctly. Also, it works correctly when a user actually logs in in the browser and there’s cookies and other jazz from the browser.

The middleware code is very simple:

import { getToken } from "next-auth/jwt"
import { NextRequest, NextResponse } from "next/server"

export async function middleware(req: NextRequest) {
  const token = await getToken({ req })
  console.log("Middleware token", token)
  NextResponse.next()
}

My API request is:

GET http://localhost:3000/api/<api_path> HTTP/1.1
Content-Type: application/json
Authorization: Bearer {{token}} 

Expected behavior

I should get the token info in the middleware with just the auth header with bearer token (not only with browser session).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:27 (5 by maintainers)

github_iconTop GitHub Comments

12reactions
HydroxZcommented, Jun 28, 2022

Why is this closed? The original issue has not been resolved yet

8reactions
AlphaBryancommented, Jul 25, 2022

Hmm thanks ! Buts still not working for me even with next@12.2.0 and the middleware in the root it keep asking for authentification

Update : I comfirm that downgrading to @12.2.0 make all working for now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auth header not being sent with GET requests - Stack Overflow
If you use a browser, the express-jwt middleware says no token was supplied and if I inspect the request headers, there isn't any...
Read more >
API Error: No authorization token was found - Auth0 Community
It sounds like there is no Authorization header being included in the request and thus no authorization token - Basically, the middleware is ......
Read more >
JWT Bearer token authentication for Express JS via ... - Medium
After executing the verify token middleware next() command is ready to execute another middleware next to the verifyToken. But in the get request,...
Read more >
Security - First Steps - FastAPI
It doesn't matter what you type in the form, it won't work yet. ... If the token contains foobar , the content of...
Read more >
Laravel Sanctum - The PHP Framework For Web Artisans
First, Sanctum is a simple package you may use to issue API tokens to your ... your api middleware group within your application's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found