143960ce by Adam Heath

API CHANGE: login/logout now return a deferred.

1 parent a6c22fd0
......@@ -78,6 +78,7 @@ define(function(require) {
}
}, this);
this.login = function() {
var deferred = $.Deferred();
fbLoaded.done(function(FB) {
FB.login(function(response) {
self.set({
......@@ -85,11 +86,13 @@ define(function(require) {
'authResponse': response.authResponse,
});
self.trigger('provider:logged-in');
deferred.resolve();
});
});
return self;
return deferred.promise();
};
this.logout = function() {
var deferred = $.Deferred();
fbLoaded.done(function(FB) {
if (self.get('authResponse')) {
FB.logout(function(response) {
......@@ -98,12 +101,14 @@ define(function(require) {
'authResponse': null,
});
self.trigger('provider:logged-out');
deferred.resolve();
});
} else {
self.trigger('provider:logged-out');
deferred.resolve();
}
});
return self;
return deferred.promise();
};
this.attachProviderData = function(data) {
var authResponse = this.get('authResponse') || {};
......@@ -125,11 +130,11 @@ define(function(require) {
this.login = function() {
// TODO: possibly fetch userName/password from browser.localStorage
this.trigger('provider:logged-in');
return this;
return $.Deferred().resolve().promise();
};
this.logout = function() {
this.trigger('provider:logged-out');
return this;
return $.Deferred().resolve().promise();
};
this.attachProviderData = function(data) {
_.extend(data, {
......@@ -235,18 +240,18 @@ define(function(require) {
})).always(function() {
ready.resolve();
});
var providers = this.get('providers');
_.each(providers.keys(), function(providerKey) {
providers.get(providerKey).on('provider:logged-in', function() {
api('auth:login:' + providerKey, addProviderData(providerKey));
});
});
}, this));
this.login = function(how) {
var provider = how ? this.get('providers').get(how) : null;
var result;
if (provider) {
provider.login();
result = provider.login().then(function() {
return api('auth:login:' + how, addProviderData(how));
});
} else {
result = $.Deferred().resolve().promise();
}
return result;
};
this.logout = function() {
_.invoke(this.get('providers').values(), 'logout');
......