0

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
         
1

Try using this:- use-forwarded-headers

apiVersion: v1
kind: ConfigMap
data:
  use-forwarded-headers: "true"
metadata:
  name: nginx-configuration
  namespace: default
2
  • it did not work. – Yilmaz May 17 at 0:16
  • 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 May 17 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.