I am using JWT for authenticating requests for the back-end api and it results in a unexpected error.
const generateJwt = function(rows) {
var expiry = new Date();
expiry.setDate(expiry.getDate() + 7);
return jwt.sign({
_id: 1,
email: rows.email,
name: rows.username,
exp: parseInt(expiry.getTime() / 1000),
}, "MY_SECRET"); // DO NOT KEEP YOUR SECRET IN THE CODE!
};
I am using the above code to generate the jwt and it generates successfully.
var jwt = require('express-jwt');
var auth = jwt({
secret: 'MY_SECRET',
userProperty: 'payload'
});
var router = express.Router();
router.get('/list',auth, controller.getMerchantList);
after a user is logged in I return the jwt token and I send the token when the client makes calls to the back-end.
$http.get('/api/merchant/list',{
headers: {
Authorization: 'Bearer '+ authentication.getToken()
}
});
The generated token : eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOjEsImVtYWlsIjoia2FzaGlmcm9zaGVuN0BnbWFpbC5jb20iLCJuYW1lIjoiQXJ5YSBTdGFyayIsImV4cCI6MTQ5OTg0NjEzOSwiaWF0IjoxNDk5MjQxMzM5fQ.-CCQwiadozSOuuIk9fil4aJh8D38NwgKYP3HpvClyKw
But I still get a 401. The jwt token is there in the front-end. I verified through a console.log. Can someone please help me solve this.