I'm trying to make my code work (it actually works in another part of my code, IDK why it isn't working here). Any help or advice would be greatly appreciated.
exports.crearPedido = async function crearPedido(req, res, next){
let {email, codProducto, cantidad, direccion, formaPago, estado } = req.body;
let emailHeaders = req.headers.email
let emailCorrecto = ""
let jsonPayload = undefined
let token = req.headers.authorization
let userEmail
//console.log(codProducto)
if (email === undefined) {
emailCorrecto = emailHeaders
} else if(emailHeaders === undefined) {
emailCorrecto = email
} else if(emailHeaders === undefined && email === undefined){
token = req.headers.authorization.split(" ")[1];
jwt.verify(token, process.env.JWT_SECRET_KEY, (err, authData) => {
let base64Url = token.split('.')[1];
let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
jsonPayload = decodeURIComponent(Buffer.from(base64, 'base64').toString().split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
console.log(jsonPayload)
jsonPayload = jsonPayload.split(':')[1]
jsonPayload = jsonPayload.split(',')[0]
jsonPayload = jsonPayload.split('"')[1]
jsonPayload = jsonPayload.split('"')[0]
console.log(jsonPayload)
})
}else {
res.status(403).json("Error de credencial de usuarios")
}
console.log(jsonPayload)
userEmail = await usuarios.findOne({auth0Id: {$eq: jsonPayload}} ,{'_id': false})
This is Part of my code, not all of it, but I guess you guys can get an idea of what's my point. The last console.log returns "undefined" or whatever data I initialize that variable with, but the one before returns the proper data. It seems like the jsonPayload variable is changed within the jwt.verify function but gets back to whatever data I use to initialize it outside of it.