0

I have a LoginFactory which returns an auth object with a token as a part of it. I need to set this token in the header of any REST API call I make, post the user logging in.

What is the best way to share this token across factoires so that the value can be used in said factories?

I tried using angular.module.value but this does not seem to work. (The value never gets set.)

Venkat
  • 1,095
  • 2
  • 13
  • 25
  • Please see this link http://stackoverflow.com/questions/11176330/angularjs-how-to-send-auth-token-with-resource-requests – Archana Jul 29 '14 at 12:06
  • @user3644619: This helps in adding a custom URL query param. This solution is not amenable to add a custom header. – Venkat Jul 29 '14 at 16:03

2 Answers2

0

in your factory make two method

_gettoken(){

return token;
}
_settoken(token_temp){
  token=token_temp;

}

now use this factory method to use it any where

Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
0

I hope it will work

app.factory('authInterceptorService', ['$q', '$location', 'localStorageService', function ($q, $location, localStorageService) {

var authInterceptorServiceFactory = {};

var _request = function (config) {

    config.headers = config.headers || {};

    var authData = localStorageService.get('authorizationData');
    if (authData) {
        **config.headers.Authorization = 'Bearer ' + authData.token;**
    }

    return config;
}

var _responseError = function (rejection) {
    if (rejection.status === 401) {
        $location.path('/login');
    }
    return $q.reject(rejection);
}

authInterceptorServiceFactory.request = _request;
authInterceptorServiceFactory.responseError = _responseError;

return authInterceptorServiceFactory;

}]); For more details, http://bitoftech.net/2014/06/09/angularjs-token-authentication-using-asp-net-web-api-2-owin-asp-net-identity/

Archana
  • 387
  • 1
  • 5