I have a function which returns a promise which is resolved if the user is currently logged in, or waits to resolve once the user has logged in. SO you can do this...
myService.waitForLogin().then(function(){
// Subscribe to pusher.com events
});
I then use a similar call to unsubscribe from events when the user logs out.
The trouble is that I want to run my .then() code every time the user logs in, so the process is....
If the user is logged in, run the code.
If not, wait till they log in, then run the code.
If they log out then back in again, run the code again.
I was originally using $rootscope.$on to listen to login/out events which worked well for the repeated occurrences, but I needed it to be promise based in case the user was already logged in.
Here is my code:
var defer = $q.defer();
if(isLoggedIn()){
defer.resolve();
}
else{
var offLoggedInTrue = $rootScope.$on('loggedin-true', function(){
defer.resolve();
offLoggedInTrue();
});
}
return defer.promise;