0

The problem that I am trying to solve is maintaining information about a user across a connection. In a request there are various commands which get independently processed one by one. In one command the user can supply credentials and get a token back, and then in subsequent requests they can supply this token to authenticate themselves. But I don't want them to have to supply the token multiple times in a single request: I want a function to know that the token has already been supplied and use it. However, there are other modules in use which don't know anything about the current request connection, they simply accept a token and other arguments and do their work.

In this related questions, Is it possible to declare global variables in Node/Express 4.0, it appears to ask about global variables, not specified to the current connection. Global Variable in app.js accessible in routes? seems to ask a similar question. However obviously this is not appropriate if there are multiple users, because credentials for each user need to apply onto to and for the duration of their connection.

How can I create a "global" variable that modules that don't know anything about the current connection can look at to determine information about the current connection only?

Community
  • 1
  • 1
Michael
  • 9,060
  • 14
  • 61
  • 123
  • What code do you have so far? – Rushy Panchal May 15 '16 at 15:42
  • @RushyPanchal I experimented a bit with creating a property in the `req` object until I realized the other module doesn't have access to this variable. Right now all requests look like an array of objects each object containing request parameters, each object currently has to supply a token, and dispatch simply calls a function in the external module passing parameters pulled from the object. – Michael May 15 '16 at 15:46
  • 2
    A property on `req` is the typical way to handle this; passing the property's value into functions as a parameter that don't have access to `req`. – JohnnyHK May 15 '16 at 15:50
  • @JohnnyHK Oh, I guess I could explicitly check whether this property exists before dispatching to the external module... – Michael May 15 '16 at 15:53

0 Answers0