Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

541
Views
get method with jwt authorization is returning empty object, do we need to decode the jwt token?

router and controller code is given below: tried using jwt decoder, it provides object : {_id, iat, exp} but in postman it return the empty object

===router===
 router.get("/secret", requireSignin, (req, res) => {

   res.json({
     user: req.user,
    });
  });
 
 
 ===controller===
 exports.requireSignin = expressJwt({
   secret: process.env.JWT_SECRET,
   algorithms: ["HS256"],
   userProperty: "auth",
 });```
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

For me, this happened when I did not async-await a function that was going to return a signed token that I wrote against a MongoDB schema like this

const sendCustomerToken = (customer, statusCode, res) => { const token = customer.getSignedToken(); res.status(statusCode).json({ success: true, token }); };

Basically, it's returning a promise, if you don't await it, it will return a null or empty object because it is procedural https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/Empty

however if you await it, it waits for the time the string is available then return it like this

const sendCustomerToken = async (customer, statusCode, res) => { const token = await customer.getSignedToken(); res.status(statusCode).json({ success: true, token }); };

https://jwt.io/introduction.

schema method

customerAuthSchema.methods.getSignedToken = async function() { return jwt.sign({ id: this._id }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRE, }); };

you may want to check promises in your code where you may be querying the DB.

also if decode is not working, try using jwt sign method which recieves a payload, secret and option - payload is the protected data you want to return, secret is your normal string you can generate using crypto and option is simply the life time of your token. like so

function() {
 return jwt.sign({ id: this._id }, process.env.JWT_SECRET, { expiresIn: process.env.JWT_EXPIRE, });};`

with env variables like this

PORT=5000 JWT_SECRET=4de5b205fa171927adb1444c06bd990fadd45ffe7a1309def8b5a JWT_EXPIRE=10min

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!