DEV Community

John Michael Biddulph
John Michael Biddulph

Posted on

req.userId is undefined

import jwt from "jsonwebtoken";

const secret = "test";
const auth = async (req, res, next) => {
  try {
    const token = req.headers.authorization.split(" ")[1];
    const isCustomAuth = token.length < 500;
    let decodedData;
    if (token && isCustomAuth) {
      decodedData = jwt.verify(token, secret);
      req.userId = decodedData?.id;
    }
    console.log("decodedData: " + decodedData);
    next();
  } catch (error) {
    console.log(error);
  }
};

export default auth;
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jbiddulph profile image
John Michael Biddulph

and decodedData is showing Object Object