1

In nodejs created a gateway that proxies the apis based on some conditions. The gateway also checks the headers in token and if validated then set the req.user

Here is the proxy code

proxy(host) {
        // call proxy middleware with request option  
        return (req, res, next) => {
            let reqBodyEncoding;
            let reqAsBuffer = false;
            let parseReqBody = true;
             
            return default_proxy(host, {
                reqAsBuffer,
                reqBodyEncoding,
                parseReqBody,
            })(req, res, next);
        }
    }

now on my proxied service I am not getting req.user. What is the solution for this?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
  • 1
    You must pass `req.user` in the request to the target host somehow. The target host gets a fresh `req` object (assuming it is implemented in Node.js at all), therefore it cannot see the gateway's `req` object. You could, for example, set a request header `X-User: `, but then you must authenticate the request so that the target host can be sure it was made by the gateway (and not by someone who wants to impersonate the user). – Heiko Theißen Aug 04 '23 at 12:20
  • any way if i can pass the user as req.user and what is the best practice? – Sunil Garg Aug 04 '23 at 12:27
  • I don't understand: The gateway makes an HTTP request to the target host, not a Javascript call. Therefore you cannot pass on the `req` object. – Heiko Theißen Aug 04 '23 at 12:28
  • yes to my another api server, like it is passing everything headers and body. so i thouht my custom object will be passed to o set on req – Sunil Garg Aug 04 '23 at 12:34
  • 1
    The other api server gets a fresh `req` object, nothing is passed on. That's how express works. – Heiko Theißen Aug 04 '23 at 12:36

0 Answers0