const verifyToken = async (req, res, next) => {
try {
if(req.headers["authorization"] && req.headers.authorization) {
const token = req.headers["authorization"].split(" ")[1];
if(token) {
const user_data = await User.findOne({ auth_token: token });
if(user_data) {
const jwtEmail = user_data?user_data.email.split("@")[0]:'';
const decodedToken = jwt.verify(token, jwtEmail);
next();
} else {
return res.json({
status:400,
message:"invalid Request"
})
}
} else {
return res.json({
status:400,
message:"invalid Token "
})
}
} else {
return res.json({
status:400,
message:"invalid Request"
})
}
} catch (error) {
//console.log(error)
return res.status(400).send({
message: "Invalid token",
error
});
}
}
exports.createOrder = async (req, res) => {
const body = req.body;
let Stack;
let locked_bal = 0;
let c_type = body.type=='buy'?body.compare_currency:body.currency_type;
let unique_id;
// if (parseFloat(body.raw_price)<200 && body.currency_type == 'rbc') {
// return res.json({
// status: 400,
// error: true,
// message: "Price is grater than 200.",
// });
// }
const token = req.headers.authorization.split(" ")[1];
const user_data = await Users.findOne({auth_token:token})
const user_id = user_data.user_id
i send Jwt Token In post man but error show in invalid token but check in database token are same in those send token and console the token in coneole.log are same token in database but not verify in verifytoken but send same token in database
2
Answers
Check your token variable where you do the split, maybe you have done an incorect split and the error you get is from the res.json invalid token.
The question is not clear, need to provide more info
JWT verification does not require matching with THE DB, actually, JWT is a signature
the public already included in the JWT,
Note: maybe you wrongly generated the token (expired)