Advertisement
ShawnsSpace

Example Login flow for Facebook JS SDK 2.5

Jun 12th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <div id="fb-root"></div>
  2. <script>
  3.   // This is called with the results from from FB.getLoginStatus().
  4.   function statusChangeCallback(response) {
  5.     console.log('statusChangeCallback');
  6.     console.log(response);
  7.     // The response object is returned with a status field that lets the
  8.     // app know the current login status of the person.
  9.     // Full docs on the response object can be found in the documentation
  10.     // for FB.getLoginStatus().
  11.     if (response.status === 'connected') {
  12.       // Logged into your app and Facebook.
  13.      
  14.       testAPI();
  15.       document.getElementById('status').innerHTML += '<button onclick="userlogout();checkLoginState();">Logout</button>';
  16.     } else if (response.status === 'not_authorized') {
  17.       // The person is logged into Facebook, but not your app.
  18.       document.getElementById('status').innerHTML = '<button onclick="userlogin();checkLoginState();">Login</button>';
  19.     } else {
  20.       // The person is not logged into Facebook, so we're not sure if
  21.       // they are logged into this app or not.
  22.       document.getElementById('status').innerHTML = '<button onclick="userlogin();checkLoginState();">Login</button>';
  23.     }
  24.   }
  25.  
  26.   // This function is called when someone finishes with the Login
  27.   // Button.  See the onlogin handler attached to it in the sample
  28.   // code below.
  29.   function checkLoginState() {
  30.     FB.getLoginStatus(function(response) {
  31.       statusChangeCallback(response);
  32.     });
  33.   }
  34.  
  35.   window.fbAsyncInit = function() {
  36.   FB.init({
  37.     appId      : '{your-app-id}',
  38.     cookie     : true,  // enable cookies to allow the server to access
  39.                         // the session
  40.     xfbml      : true,  // parse social plugins on this page
  41.     version    : 'v2.5' // use graph api version 2.5
  42.   });
  43.   //
  44.   // These three cases are handled in the callback function.
  45.  
  46.   FB.getLoginStatus(function(response) {
  47.     statusChangeCallback(response);
  48.   });
  49.  
  50.   };
  51.  
  52.   // Load the SDK asynchronously
  53.   (function(d, s, id) {
  54.     var js, fjs = d.getElementsByTagName(s)[0];
  55.     if (d.getElementById(id)) return;
  56.     js = d.createElement(s); js.id = id;
  57.     js.src = "//connect.facebook.net/en_US/sdk.js";
  58.     fjs.parentNode.insertBefore(js, fjs);
  59.   }(document, 'script', 'facebook-jssdk'));
  60.  
  61.   // Here we run a very simple test of the Graph API after login is
  62.   // successful.  See statusChangeCallback() for when this call is made.
  63.   function testAPI() {
  64.     console.log('Welcome!  Fetching your information.... ');
  65.     FB.api('/me', function(response) {
  66.       console.log('Successful login for: ' + response.name);
  67.       document.getElementById('status').innerHTML =
  68.         'Thanks for logging in, ' + response.name + '!';
  69.     });
  70.   }
  71. function userlogin(){
  72.     FB.login(function(response) {    
  73.        // handle the response, modify our button as needed.
  74.        
  75.            if (response.status === 'connected') {
  76.             // Logged into your app and Facebook.
  77.           } else if (response.status === 'not_authorized') {
  78.             // The person is logged into Facebook, but not your app.
  79.           } else {
  80.             // The person is not logged into Facebook, so we're not sure if
  81.             // they are logged into this app or not.
  82.           }
  83.        
  84.      }, {scope: 'email'});
  85. }
  86.  
  87. function userlogout(){
  88.     FB.logout(function(response) {
  89.        // Person is now logged out
  90.     });
  91. }
  92.  
  93. function removePerm(perm){
  94.     FB.api('/me/permissions/'+perm+'', 'delete', callback);
  95.     function callback(response) {
  96.     //document.getElementById(''+perm+'').innerHTML = "" + response;
  97.     document.location.reload();
  98.     }
  99. }
  100. </script>
  101.  
  102. <div id="status"></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement