Created
October 20, 2013 21:46
-
-
Save marcofucci/7075724 to your computer and use it in GitHub Desktop.
allauth.facebook refactored
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% load url from future %} | |
{% load staticfiles %} | |
<div id="fb-root"></div> | |
<script type="text/javascript" src="{% static "facebook/js/fbconnect.js" %}"></script> | |
<script type="text/javascript"> | |
allauth.facebook.init({ appId: '{{facebook_app.client_id}}', | |
locale: '{{facebook_jssdk_locale}}', | |
loginOptions: {{fb_login_options}}, | |
loginByTokenUrl: '{% url 'facebook_login_by_token' %}', | |
channelUrl : '{{facebook_channel_url}}', | |
cancelUrl: '{% url 'socialaccount_login_cancelled' %}', | |
logoutUrl: '{% url 'account_logout' %}', | |
errorUrl: '{% url 'socialaccount_login_error' %}', | |
csrfToken: '{{csrf_token}}' }); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
"use strict"; | |
function postForm(action, data) { | |
var f = document.createElement('form'); | |
f.method = 'POST'; | |
f.action = action; | |
for (var key in data) { | |
var d = document.createElement('input'); | |
d.type = 'hidden'; | |
d.name = key; | |
d.value = data[key]; | |
f.appendChild(d); | |
} | |
document.body.appendChild(f); | |
f.submit(); | |
} | |
var allauth = window.allauth = window.allauth || {}; | |
allauth.facebook = { | |
init: function(opts) { | |
this.opts = opts; | |
window.fbAsyncInit = function() { | |
FB.init({ | |
appId : opts.appId, | |
channelUrl : opts.channelUrl, | |
status : true, | |
cookie : true, | |
oauth : true, | |
xfbml : true | |
}); | |
}; | |
(function(d){ | |
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} | |
js = d.createElement('script'); js.id = id; js.async = true; | |
js.src = "//connect.facebook.net/"+opts.locale+"/all.js"; | |
d.getElementsByTagName('head')[0].appendChild(js); | |
}(document)); | |
}, | |
login: function(nextUrl, action, process) { | |
var self = this; | |
if (action == 'reauthenticate') { | |
this.opts.loginOptions.auth_type = action; | |
} | |
FB.login(function(response) { | |
if (response.authResponse) { | |
self.onLoginSuccess.call(self, response, nextUrl, process); | |
} else if (response && response.status && ["not_authorized", "unknown"].indexOf(response.status) > -1) { | |
self.onLoginCanceled.call(self, response); | |
} else { | |
self.onLoginError.call(self, response); | |
} | |
}, self.opts.loginOptions); | |
}, | |
onLoginCanceled: function(response) { | |
window.location.href = this.opts.cancelUrl; | |
}, | |
onLoginError: function(response) { | |
window.location.href = this.opts.errorUrl; | |
}, | |
onLoginSuccess: function(response, nextUrl, process) { | |
var data = { | |
next: nextUrl || '', | |
process: process, | |
access_token: response.authResponse.accessToken, | |
expires_in: response.authResponse.expiresIn, | |
csrfmiddlewaretoken: this.opts.csrfToken | |
}; | |
postForm(this.opts.loginByTokenUrl, data); | |
}, | |
logout: function(nextUrl) { | |
var self = this; | |
FB.logout(function(response) { | |
self.onLogoutSuccess.call(self, response, nextUrl); | |
}); | |
}, | |
onLogoutSuccess: function(response, nextUrl) { | |
var data = { | |
next: nextUrl || '', | |
csrfmiddlewaretoken: this.opts.csrfToken | |
}; | |
postForm(this.opts.logoutUrl, data); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment