This question has been asked multiple times, but I'm still struggling to find the root cause of the issue. I have already signed some data with a token, but when I attempt to verify it, I receive an error message saying "jwt malformed". Interestingly, both the token received from the authheader and the "secret token" specified in my dotenv file appear identical: the token from the authheader is
1e0af40b5849caa62d2bd4a65fddc832b027034fe656d50003b86e1417af6491c944b9ed936e5090d114a4c81aa09754d920daa58736f3ba6d49977cc271a0dd
, and the token in the dotenv file is also 1e0af40b5849caa62d2bd4a65fddc832b027034fe656d50003b86e1417af6491c944b9ed936e5090d114a4c81aa09754d920daa58736f3ba6d49977cc271a0dd
.
Doesn't the jwt verify method only compare whether the two strings match? Do I need additional configuration in the signing method, such as specifying the signing algorithm or type? Below is my middleware code for verification:
function authenticateToken(req , res , next){
const authHeader = req.headers.authorization;
const token = authHeader && authHeader.split(' ')[1]
console.log(token)
if(token == null) return res.status(401).send()
jwt.verify(token , process.env.ACCESS_TOKEN_SECRET , (err , user)=>{
console.log(process.env.ACCESS_TOKEN_SECRET)
console.log(err)
if(err) {return res.status(403).send()}
console.log(err)
req.new_user = user;
next()
})
}
The following code returns a value after verification :
isLoggedIn(app ,db){
app.get('/isLoggedIn' , authenticateToken, async(req ,res)=>{
await db.query('select * from client where username = $1' , [req.new_user.name] , (err , data)=>{
res.json(data.rows[0])
})
})
}
I don't believe the issue lies within this part of the code since:
logging_auth(app ,db){
app.post('/logging_auth' , async(req ,res)=>{
let credential = req.body
let email = credential.login_email;
let password = credential.login_password
let email_cols = [email];
await db.query('select client_password , username from client where email = $1' , email_cols , async(err , data)=>{
if(data && data.rows.length === 0){
res.json({
success : false,
msg : 'email or password does not exist'
})
}
if(data && data.rows.length === 1){
bycrypt.compare(password , data.rows[0].client_password , (bcrypterr , verified)=>{
//if verified gives token
if(verified){
const new_user = {name : data.rows[0].username}
jwt.sign(new_user , process.env.ACCESS_TOKEN_SECRET)
res.json({access_token : process.env.ACCESS_TOKEN_SECRET , success : true , use:new_user.name})
}else{
console.log(bcrypterr)
}
//else response success false
})
}
if(err){
res.json({
success : false,
msg : 'Opps Something Went Wrong',
status : 501
})
}
})
})
}
The error only occurs during the verification process.