In my MEAN
application, I am validating the username and password. later I am trying to get the user information to display in the web page. But I am not getting the data as readable.
I don't know the issue here and the wrong what i do. any one help me.
my response api is :
apiRoute.get('/me', function(req, res) {
res.send(req.decoded);
})
from the req.decoded
I am getting the result as :
{
"data": {
"iat": 1474893984,
"exp": 1474937184
},
"status": 200,
"config": {
"method": "GET",
"transformRequest": [
null
],
"transformResponse": [
null
],
"cache": true,
"url": "/api/me",
"headers": {
"Accept": "application/json, text/plain, */*",
"x-access-token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0NzQ4OTM5ODQsImV4cCI6MTQ3NDkzNzE4NH0._Qs2rTaH_oCAZ0c1LOe5oFiUVOZ9jXtpWc0fSykH4Xw"
}
},
"statusText": "OK"
}
In the data, i am not getting the user name, instead of getting some num values.
here is how i am setting the decode
when user logs in:
apiRoute.use(function( req, res, next ) {
var token = req.body.token || req.param('token') || req.headers['x-access-token'];
if( token ) {
jwt.verify( token, superSecret, function( err, decoded ) {
if( err ) {
return res.status(403).send({
success: false,
message: 'Failed to authenticate token.'
});
} else {
req.decoded = decoded; //setting the value on login.
next();
}
})
} else {
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
})
UPDATE
As per hpavlino suggession i have updated my code, and I am getting this in console, if any one find the solution let me know:
express deprecated req.param(name): Use req.params, req.body, or req.query instead app\routes\api.js:67:37
token is eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0NzQ5MDAzMDEsImV4cCI6MTQ3NDk4NjcwMX0.8gjrBcX6cD74pix1weqv06qoqw1xFcFHLoR3Mp2fUSE
Access token has expired
GET /api/me 200 19.364 ms - -
GET /app/views/pages/families.html 304 21.319 ms - -
GET /favicon.ico 304 5.999 ms - -
I used this method and it decoded right information:
if (token) {
try {
var decoded = jwt.decode(token, jwtSecret)
if (decoded.exp <= Date.now()) {
console.log("Access token has expired")
return next()
}
console.log(decoded);
} catch (err) {
console.log("couldn't decode token: " + err)
return next()
}
} else {
console.log("token not found!")
return next()
}