Skip to content

Instantly share code, notes, and snippets.

@jrhe
Created March 3, 2014 14:02
Show Gist options
  • Save jrhe/9325535 to your computer and use it in GitHub Desktop.
Save jrhe/9325535 to your computer and use it in GitHub Desktop.
import Facebook from 'appkit/initializers/facebook';
var FacebookAuthenticator = Ember.SimpleAuth.Authenticators.OAuth2.extend({
authenticate: function() {
var _this = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
_this.facebookCallback().then(function(response) {
var expiresAt = _this.absolutizeExpirationTime(response.expires_in);
_this.scheduleAccessTokenRefresh(response.expires_in, expiresAt, response.refresh_token);
resolve(Ember.$.extend(response, { expires_at: expiresAt }));
});
} else if (response.status === 'unauthorized') {
reject("YOU ARE UNAUTHORIZED");
} else {
FB.login(function(response) {
if (response.authResponse) {
_this.facebookCallback().then(function(response) {
var expiresAt = _this.absolutizeExpirationTime(response.expires_in);
_this.scheduleAccessTokenRefresh(response.expires_in, expiresAt, response.refresh_token);
resolve(Ember.$.extend(response, { expires_at: expiresAt }));
});
} else {
reject("Could not login to facebook.");
}
});
}
});
});
},
facebookCallback: function() {
return ic.ajax('/api/auth/facebook/callback');
}
});
export default FacebookAuthenticator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment