Utilizing an angularjs service that utilizes restangular for API calls.
angular.module('app.userService', [])
.factory('userService', ['localStorageService', '$rootScope', 'Restangular',
function(localStorageService, $rootScope, Restangular) {
function checkIfLoggedIn() {
return localStorage.getItem('token');
}
function login(email, password, onSuccess, onError) {
Restangular.all('api/authenticate')
.post({
email: email,
password: password
})
.then(
function(response) {
localStorage.setItem('token', response.token);
onSuccess(response);
},
function(response) {
onError(response);
});
}
function logout() {
localStorageService.remove('token');
}
function getCurrentToken() {
return localStorage.getItem('token');
}
function getAuthenticatedUser(onSuccess, onError) {
Restangular.one('api/authenticated_user?token=' + getCurrentToken()).get().then(function(response) {
onSuccess(response.user);
}, function(response) {
onError(response);
});
}
Restangular.setDefaultHeaders({
'Authorization': 'Bearer ' + getCurrentToken()
});
return {
checkIfLoggedIn: checkIfLoggedIn,
login: login,
logout: logout,
getCurrentToken: getCurrentToken,
getAuthenticatedUser: getAuthenticatedUser
};
}
]);
Encountering a TypeError when calling userSvc.getAuthenticatedUser() in a controller: "onSuccess is not a function" This is how the call is made:
console.log(userSvc.getAuthenticatedUser());
Seeking assistance with resolving this issue. Using angular 1.6.