I have a url:
http://localhost:3000/api/user/passwordreset/6/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NiwiZW1haWwiOiJkZWVwYWsudkBtc21leC5pbiJ9.S2EG83nz7R1xolHOp8RBPIrf-B3AG_Y3THcIRlsIqcQ
In above url, id
is 6
and token
is eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NiwiZW1haWwiOiJkZWVwYWsudkBtc21leC5pbiJ9.S2EG83nz7R1xolHOp8RBPIrf-B3AG_Y3THcIRlsIqcQ
When I am trying to access the id using req.params.id
it properly returns me value 6
But when I am trying to access the token using req.params.token
it returns undefined
.
I tried JSON.stringify the parameter and then printed it, the result is again same:
req.params:
{"id":"6","token":"undefined"}
My code is as follows:
router.get('/passwordreset/:id/:token', async (req, res) => {
console.log(`req.params: \n ` + JSON.stringify(req.params))
let id = req.params.id;
let token = req.params.token;
console.log(`id from get request: ${id}`)
console.log(`token from get request: ${token}`)
const result = await helper.checkIdExists(id)
if (result === null) {
let error = {}
error.isError = true
error.message = 'User id did not exists'
res.status(500).json(error)
} else {
console.log("id check is successful")
console.log("-----------------------------------------")
// Decrypt one-time-use token using the user's
// current password hash from the database
console.log(result)
console.log('$$$$$$$$$$$$$$$$$')
let secret = result[0].password
console.log(secret)
console.log(token)
console.log(req.params.token)
var payload = jwt.decode(token, secret);
console.log(payload);
if (typeof payload === "object") {
//TODO: Password change web link
res.redirect()
} else {
res.status(400).send("id / token is incorrect")
}
}
});
Can someone please tell me what mistake I am making here?
NOTE: Just realised that whatever second parameter I am passing in url is always undefined