Next.js Discord

Discord Forum

Middleware can't access variables?

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
I have a file stored in "@/lib/sessions.js
Its a file I use to handle my sessions.
In my session.js file, I have a map called "sessionMap" which stores key(sessionIds) and value(sessions).
In this file, I also have a function called DebugSessions() which tells me the size of the "sessionMap" variable.
While passing through the middleware, the size of "sessionMap" is always 0 but if its called elsewhere (for example: route handler) the current size of the "sessionMap" is logged.
I'm assuming that middlewares can't interact with sessionMap for some reason?
Whats the work around to this?

4 Replies

what exactly are you trying to do?
@Rafael Almeida what exactly are you trying to do?
Sun bearOP
My goal is to validate my user before I allow access to specific routes.
Upon login they have a sessionId stored in their cookies.
The server holds session data for all users in memory.
The middleware validates whether the session is valid or invalid by doing a look-up in the "sessionMap"