Next.js Discord

Discord Forum

Binding data to request via middleware to be used in an API route

Unanswered
Norwegian Forest Cat posted this in #help-forum
Open in Discord
Norwegian Forest CatOP
I have a middleware.js that I'd like to access data from within app/api/**/route.js.

Previously I would have used request.locals.* but I can't seem to get this working with Next.

What is the expected practice for this?

46 Replies

Norwegian Forest CatOP
Nextresponse.next({ locals: { userId } }) does not work. request.locals in the route handler comes back as undefined.
Norwegian Forest CatOP
Correct, but I want to be able to bind a new property to request to be used in the handler. It's a common pattern
For instance, here's how Express uses locals:
https://expressjs.com/en/5x/api.html#app.locals
Norwegian Forest CatOP
No, I'm using the built in node server with Next with AuthJS. But I think you're missing the point
yeah I don't get it
and I don't think you could do that with next
Norwegian Forest CatOP
I think you used to be able to, just not sure on how to do it these days
@Norwegian Forest Cat I think you used to be able to, just not sure on how to do it these days
you may be able to use it with page router but I don't think you can bind the data from middleware
and Im pretty sure you cant do that with app router
Norwegian Forest CatOP
Fair enough, was just trying to avoid reaching out to the database to get the user twice
rather than just verifying and getting in middleware then binding to request, then accessing that property in the API route
@Norwegian Forest Cat Fair enough, was just trying to avoid reaching out to the database to get the user twice
yeah, most people trying to avoid that but we're still not able to find away
you could pass some information with header tho
Norwegian Forest CatOP
yeah, thought about that. just seems a bit hacky
Norwegian Forest CatOP
That discussion seems to be around frontend such as layouts and components. I'm talking about route handlers
for route handler, why would you need to hit the middlware first?
Norwegian Forest CatOP
to have guarded routes?
just do that inside
Norwegian Forest CatOP
You've missed the point again
I want to get access to the currently authenticated user without querying the database twice
This only gives you access to the user while within the middleware scope, not within the request handler?
Norwegian Forest CatOP
That's my point
middleware should be handling redirection
I don't think a route handler would need redirection
Norwegian Forest CatOP
Exactlyyyy
and nextjs is focused on serverless platform, middleware is running on edge runtime
Norwegian Forest CatOP
the middleware protects a group of API routes against unauthenticated users. but what if i need access to the current user's ID for database operations from within the route?
@Ray and nextjs is focused on serverless platform, middleware is running on edge runtime
Norwegian Forest CatOP
I'm not sure you're quite understanding what you're talking about
I don't think you need middleware for route handler
@Ray I don't think you need middleware for route handler
Norwegian Forest CatOP
that would mean putting middleware on many-many routes
single source of truth would say to put it in one place and have it waterfall down
wait, i misread
why wouldn't i need to guard my API routes?
if say, i'm updating user details, i need to protect my routes so we know which user to update?
and to stop other users from updating anyone
why would you need to guard the api route in middleware?
@Ray why would you need to guard the api route in middleware?
Norwegian Forest CatOP
huh
I mean just handle it in the api route
Norwegian Forest CatOP
HUHU
nevermind
Norwegian Forest CatOP
my fella, just leave it here