nebukad

facebook login

Aug 7th, 2016
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Social = {
  2.         init : function (){
  3.             // Load the SDK asynchronously
  4.             (function(d, s, id){
  5.                 var js, fjs = d.getElementsByTagName(s)[0];
  6.                 if (d.getElementById(id)) {return;}
  7.                 js = d.createElement(s); js.id = id;
  8.                 js.src = "//connect.facebook.net/en_US/all.js";
  9.                 fjs.parentNode.insertBefore(js, fjs);
  10.             }(document, 'script', 'facebook-jssdk'));
  11.  
  12.             window.fbAsyncInit = function() {
  13.                 FB.init({
  14.                     appId: fb_id,
  15.                     cookie: true,   // enable cookies to allow the server to access the session
  16.                     xfbml: true,    // parse social plugins on this page
  17.                     version: fb_version // use version 2.1
  18.                 });
  19.  
  20.                 $(document).trigger('fbload');
  21.             };
  22.             console.log('Social init run...')
  23.         },
  24.         login: function() {
  25.             var fbOptions = {
  26.                 scope: 'email,public_profile'
  27.             };
  28.             /*
  29.              {
  30.              email: "[email protected]"
  31.              first_name: "xxx"
  32.              gender: "xxx"
  33.              id: "xxx"
  34.              last_name: "xxx"
  35.              link: "xxx/xxx/"
  36.              locale: "en_US"
  37.              name: "xxx"
  38.              timezone: 7
  39.              updated_time: "xxx"
  40.              verified: true
  41.              }
  42.              */
  43.             FB.login(function(response) {
  44.                 if (response.status == 'connected' && response.authResponse) {
  45.                     FB.api('/me', function(me) {
  46.                         console.log(me); // dump complete info
  47.                         var entity = {
  48.                             facebook_id   : response.authResponse.userID,
  49.                             facebook_url  : me.link,
  50.                             facebook_token: response.authResponse.accessToken,
  51.  
  52.                             firstname: me.first_name,
  53.                             lastname : me.last_name,
  54.                             email    : me.email,
  55.                             gender   : me.gender == 'male' ? 'm' : 'f',
  56.                         };
  57.                     });
  58.                 } else {
  59.                     //user hit cancel button
  60.                     console.log('User cancelled login or did not fully authorize.');
  61.                 }
  62.             }, fbOptions);
  63.         },
  64.         share: function() {
  65.             FB.ui({
  66.                 method: 'share',
  67.                 href: window.location.href
  68.             }, function(response){
  69.                 // window.location.reload();
  70.             });
  71.         },
  72.         like: function() {
  73.             FB.login(function(){
  74.                 FB.api(
  75.                    "/me/og.likes",
  76.                    "POST",
  77.                    {
  78.                       "object": window.location.href
  79.                    },
  80.                    function (response) {
  81.                       if (response && !response.error) {
  82.                           /* handle the result */
  83.                           // toastr['success']('result.error');
  84.                       }
  85.                    });
  86.             }, {scope: 'publish_actions'});
  87.         },
  88.         logout: function() {
  89.             FB.logout(function() {
  90.                 window.location.reload();
  91.             });
  92.         }
  93.     };
Advertisement
Add Comment
Please, Sign In to add comment