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

172
Views
How to know userID of logged user with JWT?

I want to know the userId with this route using or not the JWT Token each time I want to add on my database the elements linked which I click :

    .post(parseArticle, (req,res) => {


      db.query("SELECT * FROM articles WHERE id_article = ?", [[ req.articleId]], function (err,result) {
        if (err) throw err;


        let token = req.headers['x-access-token'] || req.headers['authorization']  || req.cookies.token;
        if (!!token && token.startsWith('Bearer ')) {
          token = token.slice(7, token.length);
        }

        if (token) {

          jwt.verify(token, 'RANDOM_TOKEN_SECRET', (err, decoded) => {
            if (err) {
              return res.status(401).json('token_not_valid');
            } else {
              req.userId = decoded.userId
              db.query("INSERT INTO panier_item(id_article,image,name,description,price,iduser) VALUES (?)", [[req.articleId,result[0].image, result[0].name, result[0].description, result[0].price, req.userId]], function (err,result)
              {if (err) throw err;})
              req.decoded = decoded;
            }

      // on envoie l'article ajouté à l'utilisateur
      res.status(200).json({message: "Good!"})
    })}})})

but I always get NULL when I want to INSERT INTO that req.userId : "INSERT INTO panier_item(id_article,image,name,description,price,iduser) VALUES (5, 'https://www.cdiscount.com/pdt2/6/0/6/1/300x300/spo4013771089606/rw/sponeta-table-tennis-de-table-table-ping-pong-co.jpg', 'Table de ping-pong', 'Le moment de détente que tout le monde attend entre deux pauses !', '400', NULL)"

Thanks you,

about 4 years ago · Juan Pablo Isaza
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!