Currently, I am diving into the world of AngularJS and REST. The code snippet that I'm analyzing has the term callback
repeatedly used in an authentication function. I'm curious to know if "callback" is a JavaScript or Angular keyword, or simply a custom variable specific to this code.
I'm struggling to understand how exactly callback
functions within the provided code. Despite trying to search for information on callback
specifically in relation to AngularJS, I haven't been able to find any useful results. If you'd like to take a look at the full code for the AngularJS module in question, you can access it via this link, which also includes all the code for the sample application.
Below is a glimpse at the module's code:
angular.module('auth', []).factory( 'auth',
function($rootScope, $http, $location) {
enter = function() {
if ($location.path() != auth.loginPath) {
auth.path = $location.path();
if (!auth.authenticated) {
$location.path(auth.loginPath);
}
}
}
var auth = {
authenticated : false,
loginPath : '/login',
logoutPath : '/logout',
homePath : '/',
path : $location.path(),
authenticate : function(credentials, callback) {
var headers = credentials && credentials.username ? {
authorization : "Basic "
+ btoa(credentials.username + ":"
+ credentials.password)
} : {};
$http.get('user', {
headers : headers
}).success(function(data) {
if (data.name) {
auth.authenticated = true;
} else {
auth.authenticated = false;
}
callback && callback(auth.authenticated);
$location.path(auth.path==auth.loginPath ? auth.homePath : auth.path);
}).error(function() {
auth.authenticated = false;
callback && callback(false);
});
},
clear : function() {
$location.path(auth.loginPath);
auth.authenticated = false;
$http.post(auth.logoutPath, {}).success(function() {
console.log("Logout succeeded");
}).error(function(data) {
console.log("Logout failed");
});
},
init : function(homePath, loginPath, logoutPath) {
auth.homePath = homePath;
auth.loginPath = loginPath;
auth.logoutPath = logoutPath;
auth.authenticate({}, function(authenticated) {
if (authenticated) {
$location.path(auth.path);
}
})
// Guard route changes and switch to login page if unauthenticated
$rootScope.$on('$routeChangeStart', function() {
enter();
});
}
};
return auth;
});
Extra Insight
In response to @okonyk's input, I have included code from another module that invokes the auth.authenticate() function:
$scope.login = function() {
auth.authenticate($scope.credentials, function(authenticated) {
if (authenticated) {
//do some stuff
$scope.error = false;
} else {
$scope.error = true;
}
})
}
I am trying to grasp the process behind the call from login()
to
auth.authenticate($scope.credentials, function(authenticated)
. How does the function(authenticated)
parameter impact the functionality inside auth.authenticate()
? Does it simply send a boolean value to determine whether the callback should be executed? Any clarification on this matter would be highly appreciated.
If you wish to examine the code of the other module containing the login()
method, feel free to click on this link: Sample App Navigation Module Code.