Skip to content

Instantly share code, notes, and snippets.

@Lazhari
Created July 19, 2015 03:20
Show Gist options
  • Save Lazhari/bc202b94c5651016cf39 to your computer and use it in GitHub Desktop.
Save Lazhari/bc202b94c5651016cf39 to your computer and use it in GitHub Desktop.
HTTP Interceptor to consume a JWT token -- Angularjs Factory
/**
* Created by dark0s on 18/07/15.
*/
angular
.module('app').config(function($urlRouterProvider, $stateProvider, $httpProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('main', {
url:'/',
templateUrl: '/views/main.html'
});
$httpProvider.interceptors.push('authInterceptor');
})
.constant('API_URL', "http://localhost:3000/");
'use strict';
angular.module('app').factory('authInterceptor', function (authToken) {
return {
request: function(config) {
var token = authToken.getToken();
if(token)
config.headers.Authorization = 'Bearer' + token;
return config;
},
response: function(response) {
return response;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment