facebook_auth.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. $(document).ready(function() {
  2. window.fbAsyncInit = function() {
  3. var fbLogin;
  4. FB.init({
  5. appId: _pm.FACEBOOK_APP_ID,
  6. cookie: true
  7. });
  8. fbLogin = function(redirect_url) {
  9. FB.login(function(response) {
  10. var url;
  11. if (response.authResponse) {
  12. url = '/auth/facebook/callback';
  13. if (redirect_url) {
  14. url = "" + url + "?redirect=" + redirect_url;
  15. }
  16. window.location = url;
  17. }
  18. }, { scope: 'email,user_birthday' });
  19. };
  20. // Auto redirect
  21. if (_pm.REDIRECT_URL) {
  22. fbLogin(_pm.REDIRECT_URL);
  23. }
  24. // Signin
  25. $('.facebook_sign_in').click(function(e) {
  26. var redirect_url = $(this).attr('data-fb-redirect');
  27. e.preventDefault();
  28. fbLogin(redirect_url);
  29. });
  30. // Signout
  31. $('#facebook_sign_out').click(function(e) {
  32. FB.getLoginStatus(function(response) {
  33. if (response.authResponse) {
  34. FB.logout();
  35. }
  36. });
  37. return true;
  38. });
  39. };
  40. $('body').prepend('<div id="fb-root"></div>');
  41. $.ajax({
  42. url: "" + window.location.protocol + "//connect.facebook.net/en_US/all.js",
  43. dataType: 'script',
  44. cache: true
  45. });
  46. });