I am currently working on a client with angularjs 1.5.8, and I have a service called AuthenticationService.
discovrApp.factory('AuthenticationService', function (
$http,
$rootScope,
$localStorage,
jwtHelper,
apiURL){
var service = {};
var config = {headers: {
Authorization: '',
'Content-Type': 'application/json'
}
};
service.Login = Login;
service.Logout = Logout;
service.SignUp = SignUp;
service.Profile = Profile;
service.ChangePassword = ChangePassword;
service.ResetPassword = ResetPassword;
service.GetProfile = GetProfile;
//service.SignUp = SignUp;
//service.SignUp = SignUp;
return service;
function Login(username,password,callback) {
$http.post(apiURL + 'api/rest/auth/login/', { username: username, password: password })
.success(function(response){
//login successful if there's a token in the respose
if(response.token){
//decode token, to get the user id insert on payload
var token = jwtHelper.decodeToken(response.token);
var userProfile = 0;
userProfile = GetProfile(token.user_id);
console.log(userProfile);
//store username and token in local storage to keep user logged in between paga refreshes
$localStorage.currentUser = {id: token.user_id, username: username, token: response.token };
//config.headers.Authorization = 'JWT ' + response.token;
//add jwt token to auth header for all requests made by the $http services
$http.defaults.headers.common.Authorization = 'JWT ' + response.token;
//execuete callback with true to indicate successful login
callback(true);
}else{
//execute callback with false to indicate failed login
callback(false);
}
});
}
function Logout() {
$http.post(apiURL + 'api/rest/auth/logout/')
.success(function(response){
//remove user from local storage and clear http auth header
delete $localStorage.currentUser;
localStorage.removeItem('user');
config.headers.Authorization = '';
$http.defaults.headers.common.Authorization = '';
});
}
function SignUp(callback,username,password1,password2,email,more){
$http.post(apiURL + 'api/rest/auth/registration/', { username: username, password1: password1, password2: password2, email: email})
.success(function(response){
//login successful if there's a token in the respose
if(response.token){
console.log(response.token);
//store username and token in local storage to keep user logged in between paga refreshes
$localStorage.currentUser = { username: username, token: response.token };
//add jwt token to auth header for all requests made by the $http services
$http.defaults.headers.common.Authorization = 'JWT ' + response.token;
//execuete callback with true to indicate successful login
callback(true);
}else{
//execute callback with false to indicate failed login
callback(false);
}
});
}
function Profile(){
$http.get(apiURL + 'api/user/' + $localStorage.currentUser.id + '/').
then(function successCallback(response) {
localStorage.setItem('user', JSON.stringify(response.data));
console.log(response.data)
}, function errorCallback(response) {
console.log(response);
});
}
function GetProfile(id){
$http.get(apiURL + 'api/user/' + id + '/').
then(function successCallback(response){
$rootScope.userProfile = response.data.Kind;
localStorage.setItem('profile', $rootScope.userProfile);
//console.log(($rootScope.userProfile = response.data.Kind));
});
return $rootScope.userProfile;
}
});
I have the problem with the Profile() and GetProfile() functions, when I call them for the first time they always return or assign "undefined", and if I leave a console.log in the functions to print the result of the api, first it returns undefined, and if I call it again, it returns or assigns the value, the problem is the first time I always call it, it always returns undefined and the second time it doesn't, I would like to know if anyone has happened to it and knows the reason and how to fix this.
**NOTES: In the service that I create you will be able to notice several things, and that is that I have tried different ways such as saving the data returned by the get request in a local variable, and then printing this local variable, without a positive result at the first attempt. When refreshing the page, and trying again, it returns undefined at first and then returns it.
Thanks in advance, Regards.
Your service is poorly designed. All the requests you make are missing
return $http....
the place of simply invoking$http
For example
Then when you want to call it you do this
Basically you return a promise to chain that you check once the information has come to you from the server.
Hi, you have a return problem, the second call actually returns the value of the first, which is already stored.
you should do the following
You are doing the return of the content outside of the get, so what it does is first make the call (which is async) and deliver the result that is stored... If you change it to the code I mentioned above, the function will not return anything until the get has been resolved. Cheers