Middleware can't access variables?
Unanswered
Sun bear posted this in #help-forum
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?
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
@Sun bear 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?
middlewares run in a completely different environment than the rest of the files, so they can't share the same memory. there is no workaround for this besides doing a network request to your server if you want to have persistent state across different middleware requests
@Rafael Almeida middlewares run in a completely different environment than the rest of the files, so they can't share the same memory. there is no workaround for this besides doing a network request to your server if you want to have persistent state across different middleware requests
Sun bearOP
Is that solution recommended? or would you recommend I go about what I'm trying to do via another method?
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"
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"