<p>Try using this:- <a href="https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#use-forwarded-headers" rel="nofollow noreferrer">use-forwarded-headers</a></p>
<pre><code>apiVersion: v1
kind: ConfigMap
data:
  use-forwarded-headers: &quot;true&quot;
metadata:
  name: nginx-configuration
  namespace: default
</code></pre>

CC BY-SA 4.0
2021-05-17T00:16:44.657Z
it did not work.
Yilmaz
CC BY-SA 4.0
2021-05-17T10:00:04.387Z
I seemed to have misunderstood that the currentUser is a header. It is just the body of the POST request? If so, can you see what the nginx logs say?
rohatgisanat
CC BY-SA 4.0
2021-05-16T21:33:47.947Z
2021-05-16T21:33:47.947Z
rohatgisanat
<p>This is currentUser middleware:</p>
<pre class="lang-js prettyprint-override"><code>    export const currentUser = (
      req: Request,
      res: Response,
      next: NextFunction
    ) =&gt; {
 
      if (!req.session?.jwt) {
        return next();
      }
      try {
        const payload = jwt.verify(
          req.session.jwt,
          process.env.JWT_KEY!
        ) as UserPayload;
        console.log(&quot;payload in curretnUser middleware&quot;, payload);
        req.currentUser = payload;
      } catch (err) {
        console.log(
          &quot;error in currentUser middleware in common module&quot;,
          err.message
        );
      }
      next();
    };
</code></pre>
<p>this is payload object that logged:</p>
<pre class="lang-js prettyprint-override"><code>      payload in curretnUser middleware {
      id: '609b356f99a625001a76e65e',
      email: 'test@test.com',
      iat: 1620784495
   }
</code></pre>
<p>this is the router in &quot;auth&quot; service:</p>
<pre class="lang-js prettyprint-override"><code>router.get(
  &quot;/api/users/currentuser&quot;,
  currentUser,
  (req: Request, res: Response) =&gt; {
    // if req.currentUser is not defined it will send undefined
    console.log(&quot;request to api/users/currentuser&quot;, req.currentUser);
    res.send({ currentUser: req.currentUser || null });
  }
);
</code></pre>
<p>When request hits this route, this gets logged:</p>
<pre class="lang-js prettyprint-override"><code>request to api/users/currentuser {
     currentUser: {
     id: '609b356f99a625001a76e65e',
     email: 'test@test.com',
     iat: 1620784495
   }
 }
</code></pre>
<p>How ever when I send POST request through postman I get <code>currentUser:null</code>. Somehow kubernetes or ingress-nginx is not attaching &quot;currentUser&quot; to request object and I could not figure out why. This is the nginx-ingress config:</p>
<pre class="lang-js prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-srv
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.org/client-max-body-size: &quot;4m&quot;
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    nginx.ingress.kubernetes.io/enable-cors: &quot;true&quot;
    nginx.ingress.kubernetes.io/auth-method: POST
spec:
  defaultBackend:
    service:
      name:
        nginx-ingress-default-backend
        # name: default-http-backend:80
      port:
        number: 80

  rules:
    - host: ticketing.dev
      http:
        paths:
          - path: /api/users/?(.*)
            pathType: Exact
            backend:
              service:
                name: auth-srv
                port:
                  number: 3000
         
</code></pre>

<kubernetes><node.js><ingress>
ingrex-nginx in kubernetes with express node.js does not return req.currentUser
Yilmaz
# ingrex-nginx in kubernetes with express node.js does not return req.currentUser

(Asked by: username_0 on 2021-05-16)

This is currentUser middleware:
    export const currentUser = (
      req: Request,
      res: Response,
      next: NextFunction
    ) => {
 
      if (!req.session?.jwt) {
        return next();
      }
      try {
        const payload = jwt.verify(
          req.session.jwt,
          process.env.JWT_KEY!
        ) as UserPayload;
        console.log("payload in curretnUser middleware", payload);
        req.currentUser = payload;
      } catch (err) {
        console.log(
          "error in currentUser middleware in common module",
          err.message
        );
      }
      next();
    };

this is payload object that logged:
      payload in curretnUser middleware {
      id: '609b356f99a625001a76e65e',
      email: 'test@test.com',
      iat: 1620784495
   }

this is the router in "auth" service:
router.get(
  "/api/users/currentuser",
  currentUser,
  (req: Request, res: Response) => {
    // if req.currentUser is not defined it will send undefined
    console.log("request to api/users/currentuser", req.currentUser);
    res.send({ currentUser: req.currentUser || null });
  }
);

When request hits this route, this gets logged:
request to api/users/currentuser {
     currentUser: {
     id: '609b356f99a625001a76e65e',
     email: 'test@test.com',
     iat: 1620784495
   }
 }

How ever when I send POST request through postman I get currentUser:null. Somehow kubernetes or ingress-nginx is not attaching "currentUser" to request object and I could not figure out why. This is the nginx-ingress config:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-srv
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.org/client-max-body-size: "4m"
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/auth-method: POST
spec:
  defaultBackend:
    service:
      name:
        nginx-ingress-default-backend
        # name: default-http-backend:80
      port:
        number: 80

  rules:
    - host: ticketing.dev
      http:
        paths:
          - path: /api/users/?(.*)
            pathType: Exact
            backend:
              service:
                name: auth-srv
                port:
                  number: 3000

**Tags:** kubernetes, node.js, ingress


---

## 1 answers

### Answer by username_1 on 2021-05-16. Score: 1

Try using this:- use-forwarded-headers
apiVersion: v1
kind: ConfigMap
data:
  use-forwarded-headers: "true"
metadata:
  name: nginx-configuration
  namespace: default

> **Comments:**
> **username_0**: it did not work.  
> **username_1**: I seemed to have misunderstood that the currentUser is a header. It is just the body of the POST request? If so, can you see what the nginx logs say?  

---

