0

I'am using satellizer to authenticate my app with backend using nodejs, but every http request doesn't attact req.headers.authorization, and i don't know why, please help me fix this, tks in advance

app.js

angular.module('userApp', ['angular-ladda','mgcrea.ngStrap', 'ui.router', 'satellizer', 'angular-loading-bar'])
.config(function($httpProvider, $stateProvider, $urlRouterProvider, $authProvider, $locationProvider) {

    $stateProvider
        .state('/home', {
            url: '/',
            templateUrl: 'app/views/pages/home.html'
        })

        .state('/login', {
            url: '/login',
            templateUrl: 'app/views/pages/login.html',
            controller: 'LoginCtrl',
            controllerAs: 'login'
        })

        .state('/signup', {
            url: '/signup',
            templateUrl: 'app/views/pages/signup.html',
            controller: 'SignupCtrl',
            controllerAs: 'signup'
        })

        .state('users', {
            url: '/users',
            templateUrl: 'app/views/pages/user/all.html',
            controller: 'UserCtrl',
            controllerAs: 'user',
            resolve: {
                authenticated: function($q, $location, $auth) {
                    var deferred = $q.defer();

                    if (!$auth.isAuthenticated()) {
                        $location.path('/login');
                    } else {
                        deferred.resolve();
                    }

                    return deferred.promise;
                }
            }
        });


    $authProvider.facebook({
        clientId: 'xxxxxxxxxxx'
    });

    $urlRouterProvider.otherwise('/');
    $locationProvider.html5Mode(true);

});

userController.js

angular.module('userApp')
.controller('UserCtrl', function(User, $alert) {
    vm = this;

    vm.getAllUser = function() {
        vm.processing = true;
        User.all()
            .success(function(data) {
                vm.processing = true;
                vm.users = data;
            })
            .error(function(error) {
                $alert({
                    content: error.message,
                    animation: 'fadeZoomFadeDown',
                    type: 'material',
                    duration: 3
                });
            });
    };

    vm.getAllUser();
});

userService.js

angular.module('userApp')
.factory('User', function($http) {
    return{
        all: function() {
            return $http.get('/api/all');
        }
    };
});

function for checking authentication before calling restfull api

function ensureAuthenticated(req, res, next) {
if (!req.headers.authorization) {
    return res.status(401).send({ message: 'Please make sure your request has an Authorization header' });
}

var token = req.headers.authorization;

var payload = null;
try {
    payload = jwt.decode(token, config.TOKEN_SECRET);
} catch(err) {
    return res.status(401).send({ message: err.message });
}
req.user = payload.sub;
next();

}

api

apiRouter.get('/all', ensureAuthenticated, function(req, res) {
    User.find({}, function(err, users) {
        res.send(users);
    });
});
ldmtam
  • 1
  • 1
  • Not too sure what kind of answer you are expecting here as you left out all the code that is not working? – davethecoder Jul 09 '15 at 15:59
  • i have just added my code @davethecoder, variable req.headers.authorization in ensuredAuthenticated is always null but i don't know why? – ldmtam Jul 09 '15 at 16:16

0 Answers0