Created
July 19, 2015 03:20
-
-
Save Lazhari/bc202b94c5651016cf39 to your computer and use it in GitHub Desktop.
HTTP Interceptor to consume a JWT token -- Angularjs Factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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/"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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