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,