I am encountering difficulties when trying to attach the auth0 token to a http.get request for an API that requires the token. The token is generated upon user login and stored in the browser's local storage, which is functioning properly. The challenge lies in appending the token to the http.get request.
Here is the code I currently have. In my app.js:
var QuoteApp = angular.module('QuoteApp', ['ui.router', 'auth0', 'angular-jwt', 'angular-storage', 'ngCookies']);
priceQuoteApp.config(function ($stateProvider, $urlRouterProvider, $httpProvider, authProvider, $locationProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.tokenGetter = function(store, jwtHelper, auth) {
var idToken = store.get('token');
var refreshToken = store.get('refreshToken');
if (!idToken || !refreshToken) {
return null;
}
}
$httpProvider.interceptors.push('jwtInterceptor');
});
Furthermore, I have a $http.get function sending a request to the API requiring the token.
In my api.service.js file:
this.getStuff = function (attributes) {
return $http.get('http://www.theurl.com/api/getstuff?json=' + JSON.stringify(attributes)).
success(function(){
});
};
The getStuff function is called from my getStuff.js file as follows:
$scope.getTheStuff = function (){
Services.getStuff($scope.Attributes)
.then(function (res) {
})
};
Upon making the request, I receive an authentication error from the server - 401 (Unauthorized), indicating that the token did not append to the http.get request. Any insights into this issue?
I have also provided the headers from the browser console below:
Remote Address:10.34.51.34:80
Request URL:http://www.theurl.com/api/getstuff?json={stuff in here}]}
Request Method:GET
Status Code:401 Unauthorized
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Host:http://www.theurl.com
Origin:http://localhost:63342
Referer:http://localhost:63342/price-tool-ui-1/app/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin:*