Good evening,
I have an application and I encounter some problems with my GET method when I send it. After I authenticate and I try to display the data of a list I find myself ejecting to the login page and returns a 401 error. I tested well on Postman and my server works as it should, But I think it is on the client side that it does not go … yet I do not see where the worry can be …
My service.js :
`angular.module(‘app.services’, [])
.factory(‘BlankFactory’, [function(){
}])
.service(‘LoginService’, function($http, $rootScope, $ionicLoading, $ionicPopup, API_ENDPOINT){
var LOCAL_TOKEN_KEY = ‘yourTokenKey’;
var isAuthenticated = false;
var authToken;
function loadUserCredentials() {
var token = window.localStorage.getItem(LOCAL_TOKEN_KEY);
if (token) {
useCredentials(token);
}
}
function storeUserCredentials(token) {
window.localStorage.setItem(LOCAL_TOKEN_KEY, token);
useCredentials(token);
}
function useCredentials(token) {
isAuthenticated = true;
authToken = token;
// Set the token as header for your requests!
$http.defaults.headers.common.Authorization = authToken;
}
function destroyUserCredentials() {
authToken = undefined;
isAuthenticated = false;
$http.defaults.headers.common.Authorization = undefined;
window.localStorage.removeItem(LOCAL_TOKEN_KEY);
}
var login = function($mail, $password) {
console.dir($rootScope);
var data = {
mail: $mail,
password: $password
};
$ionicLoading.show();
return $http(
{
method : 'POST',
url : API_ENDPOINT.url + '/authenticate',
headers : {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data : data
})
.then(function(response) {
var resp = response.data;
var status = response.status;
console.log("Code Status :", status);
if(status == 200) {
$rootScope.token = response.data.token;
window.localStorage.setItem('token', response.data.token);
storeUserCredentials($rootScope.token);
$rootScope.errorMessage = null;
} /*else {$rootScope.errorMessage = response.message;}*/
$ionicLoading.hide();
}, function errorCallback(response) {
$ionicLoading.hide();
console.log(response);
var alertPopup = $ionicPopup.alert({
title: 'ERROR !',
template: 'Please check your creditentials'
});
});
}
var logout = function() {
destroyUserCredentials();
};
var user = function(){
var token = window.localStorage.getItem('token');
return $http({
method : 'GET',
url : API_ENDPOINT.url + '/list'
}).then(function(result) {
console.log(result.data);
});
};
loadUserCredentials();
return {
login : login,
logout : logout,
user : user,
isAuthenticated: function() {return isAuthenticated;}
}
})
.factory(‘AuthInterceptor’, function ($rootScope, $q, AUTH_EVENTS) {
var request = function(config) {
config.headers = config.headers || {};
var token = window.localStorage.getItem(‘token’);
if (config.headers) {
config.headers.Authorization = 'Bearer ’ + token;
}
return config || $q.when(config);
};
var responseError = function (response) {
$rootScope.$broadcast({
401: AUTH_EVENTS.notAuthenticated
}[response.status], response);
return $q.reject(response);
};
return {
request : request,
responseError: responseError
};
})
.config(function ($httpProvider) {
$httpProvider.defaults.withCredentials = true;
$httpProvider.interceptors.push(‘AuthInterceptor’)
});
This is the code of my service.js, if someone could enlighten me please … it has been a while that I’m on and nothing works … Thanks in advance`