node.jsexpressexpress-jwt

express-jwt secret does not import process.env.JWT_SECRET


I'm trying to use process.env.JWT_SECRET for the secret but it just trow error:

export const requireSignin = expressjwt({
  secret: `${process.env.JWT_SECRET}`,
  algorithms: ["HS256"],
  userProperty: "auth",
});

This way I don't have error but the then I get 'UnauthorizedError: invalid signature'

export const requireSignin = expressjwt({
  secret: process.env.JWT_SECRET,
  algorithms: ["HS256"],
  userProperty: "auth",
});

This way I get "RangeError: express-jwt: secret is a required option"

export const requireSignin = () =>{

  return expressjwt({
  secret: process.env.JWT_SECRET,
  algorithms: ["HS256"], 
  userProperty: "auth",
  }); 
}

This way just load forever.

export const requireSignin = expressjwt({
  secret: '09f26e402586e2faa8da4c98a35f1b20d6b033c6097befa8be3486a829587fe2f90a832bd3ff9d42710a4da095a2ce285b009f0c3730cd9b8e1af3eb84df6611',
  algorithms: ["HS256"],
  userProperty: "auth",
});

And here is the only way working (which is problematic..) Any help is welcome, thank you.


Solution

  • 1 - Install dotenv

    2 - Require it where you need the env variable:

    require("dotenv/config");