I am working with JWT tokens with Nodejs And Angular JS. I found $httpProvider way of setting header on each request. But the headers are not being sent. My NodeJS app get undefined on accessing that header.
My angular code
For setting authorization token -
$httpProvider.interceptors.push(['$q', '$location', '$localStorage', function($q, $location, $localStorage) {
return {
'request': function (config) {
config.headers = config.headers || {};
if ($localStorage.token){
console.log($localStorage.token)
config.headers.Authorization = 'Bearer ' + $localStorage.token;
console.log(config.headers);
}
return config;
},
'responseError': function(response) {
if(response.status === 401 || response.status === 403) {
$location.path('#login');
}
return $q.reject(response);
}
};
}]);
Here is my HTTP Request
$scope.getSearcHistory = function(){
console.log($localStorage.token);
console.log( $rootScope.getCurrentUser());
$http({
url: baseUrl+'userSearches/2',
method : 'GET',
headers: {
'key': '1234424252'
}
})
.success(function(data, status, headers, config){
console.log(data);
$scope.searches = data;
})
.error(function(data, status, headers, config){
console.log(status);
});
}
This is my NodeJS Contoller
this.router.use(function(req, res, next){
console.log(req.headers);
var bearerToken;
var bearerHeader = req.headers.authorization;
console.log(bearerHeader)
if (typeof bearerHeader !== 'undefined') {
var bearer = bearerHeader.split(" ");
bearerToken = bearer[1];
req.token = bearerToken;
next();
} else {
res.send(403).json({"Authorization": "Not allowed"});
}
});
Here is my Node console, see there is no header which i need --
{
host: 'localhost:8080',
connection: 'keep-alive',
'access-control-request-method': 'GET',
origin: 'http://localhost:440',
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'access-control-request-headers': 'authorization,key',
accept: '*/*',
referer: 'http://localhost:440/vokal/'}
via Shailendra2014
No comments:
Post a Comment