143960ce by Adam Heath

API CHANGE: login/logout now return a deferred.

1 parent a6c22fd0
...@@ -78,6 +78,7 @@ define(function(require) { ...@@ -78,6 +78,7 @@ define(function(require) {
78 } 78 }
79 }, this); 79 }, this);
80 this.login = function() { 80 this.login = function() {
81 var deferred = $.Deferred();
81 fbLoaded.done(function(FB) { 82 fbLoaded.done(function(FB) {
82 FB.login(function(response) { 83 FB.login(function(response) {
83 self.set({ 84 self.set({
...@@ -85,11 +86,13 @@ define(function(require) { ...@@ -85,11 +86,13 @@ define(function(require) {
85 'authResponse': response.authResponse, 86 'authResponse': response.authResponse,
86 }); 87 });
87 self.trigger('provider:logged-in'); 88 self.trigger('provider:logged-in');
89 deferred.resolve();
88 }); 90 });
89 }); 91 });
90 return self; 92 return deferred.promise();
91 }; 93 };
92 this.logout = function() { 94 this.logout = function() {
95 var deferred = $.Deferred();
93 fbLoaded.done(function(FB) { 96 fbLoaded.done(function(FB) {
94 if (self.get('authResponse')) { 97 if (self.get('authResponse')) {
95 FB.logout(function(response) { 98 FB.logout(function(response) {
...@@ -98,12 +101,14 @@ define(function(require) { ...@@ -98,12 +101,14 @@ define(function(require) {
98 'authResponse': null, 101 'authResponse': null,
99 }); 102 });
100 self.trigger('provider:logged-out'); 103 self.trigger('provider:logged-out');
104 deferred.resolve();
101 }); 105 });
102 } else { 106 } else {
103 self.trigger('provider:logged-out'); 107 self.trigger('provider:logged-out');
108 deferred.resolve();
104 } 109 }
105 }); 110 });
106 return self; 111 return deferred.promise();
107 }; 112 };
108 this.attachProviderData = function(data) { 113 this.attachProviderData = function(data) {
109 var authResponse = this.get('authResponse') || {}; 114 var authResponse = this.get('authResponse') || {};
...@@ -125,11 +130,11 @@ define(function(require) { ...@@ -125,11 +130,11 @@ define(function(require) {
125 this.login = function() { 130 this.login = function() {
126 // TODO: possibly fetch userName/password from browser.localStorage 131 // TODO: possibly fetch userName/password from browser.localStorage
127 this.trigger('provider:logged-in'); 132 this.trigger('provider:logged-in');
128 return this; 133 return $.Deferred().resolve().promise();
129 }; 134 };
130 this.logout = function() { 135 this.logout = function() {
131 this.trigger('provider:logged-out'); 136 this.trigger('provider:logged-out');
132 return this; 137 return $.Deferred().resolve().promise();
133 }; 138 };
134 this.attachProviderData = function(data) { 139 this.attachProviderData = function(data) {
135 _.extend(data, { 140 _.extend(data, {
...@@ -235,18 +240,18 @@ define(function(require) { ...@@ -235,18 +240,18 @@ define(function(require) {
235 })).always(function() { 240 })).always(function() {
236 ready.resolve(); 241 ready.resolve();
237 }); 242 });
238 var providers = this.get('providers');
239 _.each(providers.keys(), function(providerKey) {
240 providers.get(providerKey).on('provider:logged-in', function() {
241 api('auth:login:' + providerKey, addProviderData(providerKey));
242 });
243 });
244 }, this)); 243 }, this));
245 this.login = function(how) { 244 this.login = function(how) {
246 var provider = how ? this.get('providers').get(how) : null; 245 var provider = how ? this.get('providers').get(how) : null;
246 var result;
247 if (provider) { 247 if (provider) {
248 provider.login(); 248 result = provider.login().then(function() {
249 return api('auth:login:' + how, addProviderData(how));
250 });
251 } else {
252 result = $.Deferred().resolve().promise();
249 } 253 }
254 return result;
250 }; 255 };
251 this.logout = function() { 256 this.logout = function() {
252 _.invoke(this.get('providers').values(), 'logout'); 257 _.invoke(this.get('providers').values(), 'logout');
......