Skip to content

Commit b7c78da

Browse files
DAB0mBUrigo
authored andcommitted
fix(#25): Resolve promise immediately in case user is already logged in (#29)
1 parent 60361e6 commit b7c78da

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/angular-meteor-auth.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ function($Mixer, $log) {
5757

5858
const deferred = this.$$defer();
5959

60+
// If user is already logged in resolve the promise immediately to prevent an
61+
// unnecessary computation
62+
if (this.currentUser) {
63+
deferred.resolve(this.currentUser);
64+
// Keep the schema of the promise consistent
65+
deferred.promise.stop = angular.noop;
66+
return deferred.promise;
67+
}
68+
6069
// Note the promise is being fulfilled in the next event loop to avoid
6170
// nested computations, otherwise the outer computation will cancel the
6271
// inner one once the scope has been destroyed which will lead to subscription
@@ -84,9 +93,8 @@ function($Mixer, $log) {
8493
return this.$$afterFlush(deferred.reject, error);
8594
});
8695

87-
const promise = deferred.promise;
88-
promise.stop = computation.stop.bind(computation);
89-
return promise;
96+
deferred.promise.stop = computation.stop.bind(computation);
97+
return deferred.promise;
9098
};
9199

92100
// Calls a function with the provided args after flush

tests/integration/auth.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ describe('angular-meteor.auth', function() {
193193
scope.$$afterFlush('$$throttledDigest');
194194
});
195195
});
196+
197+
it('should resolve promise immediately if user is already logged in', function(done) {
198+
Accounts.login('tempUser').onEnd(function() {
199+
var spy = jasmine.createSpy().and.returnValue(true);
200+
201+
scope.$awaitUser(spy).then(function(user) {
202+
expect(user.username).toEqual('tempUser');
203+
done()
204+
});
205+
206+
scope.$$throttledDigest();
207+
});
208+
});
196209
});
197210

198211
describe('$waitForUser()', function() {

0 commit comments

Comments
 (0)