> For the complete documentation index, see [llms.txt](https://tmdarneille.gitbook.io/seirfx/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tmdarneille.gitbook.io/seirfx/unit-2/express-auth/theory/04middleware.md).

# Middleware

We want to be able to check if our user is logged in from within any of our routes. One strategy for doing this is to expand our request object (req) using an express middleware. Middleware is code that exists in the "middle" between receiving the request/response objects from the client and handling them on the server.

We've already used 3rd party Express middleware (remember all those `app.use` statements we've written??) and we can create our own.

**Creating our own middleware**

app.use simply accepts a function as it's parameter and gives us that function 3 arguments:

* `req` - the request object
* `res` - the response object
* `next` - a callback function (moves to the next middleware)

We can modify each of these arguments and the modification will be reflected in the next middleware and finally in our route code.

**Example**

```javascript
app.use((req, res, next) => {
  req.getParamNames = () => {
    return Object.keys(req.params);
  }
  next();
});

// ...later on in your code...

app.get('/sum/:x/:y', function(req, res) {
  res.send(req.getParamNames());
});
//outputs: ['x','y']
```

Using this concept we can create a middleware to get our currently logged in user from the `req.session`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tmdarneille.gitbook.io/seirfx/unit-2/express-auth/theory/04middleware.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
