1

i'm trying to implement a JWT based authentication in my angularjs app. But i'm struggleing with the request header issues. The thing is that i have a Controller that calls a service, and this one returns a token. This step works fine.

'use strict';

angular.module("home.controllers", ['home.services', 'ngStorage'])

.controller('HomeController', ['$scope', '$location', '$http', '$localStorage', 'HomeService',
function($scope, $location, $http, $localStorage, HomeService) {


    function login(user, password) {

        HomeService.login(user, password).then(

            function(loginResult) {
                var token = loginResult.token;

                if (loged()) {
                    $localStorage.token = token;
                    $http.defaults.headers.common.Authorization = 'Bearer ' + token;
                    console.log($location.path('secure/index'));
                    $location.path('secure/index');
                }
            },

            function(err) {
                console.error(err);
            }
        );
    }

    function loged() {
        return $scope.token !== null;
    }

    return {

        loged: function() {
            return loged();
        },

        login: function(user, password)) {
        return login(user, password)
    }
};

}]);

But the thing is that I would like to redirect the user after login to "secure/index" with the "Authentication" header in the request because I have a Spring filter checking all the "*/secure" paths have that header.

The main app.js is the following

'use strict';

angular.module("home", ['ui.router', 'home.controllers', 'urlLanguageStorage', 'pascalprecht.translate', 'ui.bootstrap', 'ngStorage'])

.config(['$stateProvider', '$translateProvider', '$urlRouterProvider',
    function($stateProvider, $translateProvider, $urlRouterProvider) {

        $translateProvider.useUrlLoader("home/mensajes");
        $translateProvider.useStorage("UrlLanguageStorage");
        $translateProvider.preferredLanguage("en");
        $translateProvider.fallbackLanguage("en");

        $stateProvider
            .state("home", {
                url: "/",
                templateUrl: "resources/js/home/views/home/home.html",
                controller: "HomeController"
            });

        $stateProvider
            .state("contacto", {
                url: "/contacto",
                templateUrl: "resources/js/home/views/contact/contac.html",
                controller: "HomeController"
            });
    }
])

.run(['$localStorage', '$http', function($localStorage, $http) {

    if ($localStorage.token) {
        $http.defaults.headers.common.Authorization = 'Bearer ' + $localStorage.token;
    }
}]);

The thing is that it doesnt redirect the page. Instead of $location i tried with $window.href and it redirect the page succesfully but with no auth header in the request.

Any ideas/suggestions for that? note that I'm a begginer in Angularjs so probably there are more things wrong.

Thanks in advance

1 Answer 1

1

It looks like once you have your token, you are redirecting to secure/index with $location.path('secure/index');. However, the example code you have doesn't show a state that maps to secure/index.

Sign up to request clarification or add additional context in comments.

2 Comments

And excuse me, would you also tell me why do i need to write that thing on the . run of my module?
Upon a successful log in, the code is storing the token on the localStorage: $localStorage.token = token;. Local Storage persists across multiple page loads. The run block is checking to see if there was any previous token and using it. $http.defaults.headers.common.Authorization = 'Bearer ' + $localStorage.token; sets the authorization header for each http request. With out this line, you would have to set the authorization header manually for each request you sent to the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.