Here is a section of code that I am working with:
.factory('Auth', function($firebaseAuth) {
var ref = new Firebase('https://myfirebase.firebaseio.com');
return $firebaseAuth(ref);
})
and an accompanying controller:
.controller("LoginCtrl", function($scope, Auth, $timeout) {
function authDataCallback(authData) {
$timeout(function() {
console.log(Auth);
$scope.user = authData;
if (authData) {
console.log("Authentication successful.");
}
});
}
$scope.auth = Auth;
console.log("Adding a new callback in login");
$scope.auth.$onAuth(authDataCallback);
...
In my application, there is a possibility for the user to be redirected back to the login page and thus causing the LoginCtrl controller to be called again. The issue arises when the same callback keeps getting added every time the login page is revisited, leading to multiple executions of the callback code.
Is there a way to remove or cancel these callbacks when leaving/loading a controller?
Thank you!