codingOL

front-end code (same for all pages)

Oct 11th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.56 KB | None | 0 0
  1.         window.fbAsyncInit = function () {
  2.             FB.init({
  3.                 appId: '672221882789824', // App ID
  4.                 channelUrl: '//'+ gameForestIP +':46069/channel.html',
  5.                 status: true, // check login status
  6.                 cookie: true, // enable cookies to allow the server to access the session
  7.                 xfbml: true  // parse XFBML
  8.             });
  9.  
  10.             // Initialize when a user has authenticated with FB and this app
  11.             // Handle events accordingly
  12.             FB.Event.subscribe('auth.authResponseChange', function (response) {
  13.  
  14.                 if (response.status === 'connected') {
  15.                     // user is logged in to FB
  16.                     // has authorized app
  17.  
  18.                     console.log('Welcome, fetching information');
  19.  
  20.                     FB.api('/me', function (response) {
  21.  
  22.                         var fbid = response.id;
  23.                         var fbuser = response.username;
  24.                         var fname = response.first_name;
  25.                         var lname = response.last_name;
  26.  
  27.                         var request = $.ajax({
  28.                             url: "http://"+ gameForestIP +":1193/service/user/fb?fbid=" + fbid + "&username=" + fbuser + "&firstname=" + fname + "&lastname=" + lname,
  29.                            async: true,
  30.                            type: "POST"
  31.                        });
  32.  
  33.                         request.done(function (msg) {
  34.                             if (msg.ResponseType != 0) {
  35.                                 // show error (maybe in an error div?)
  36.                                 $("#alertDialog").show();
  37.                                 $("#alertDialog").html("<p class='glyphicon glyphicon-warning-sign' style='margin-right:10px'/>" + msg.AdditionalData);
  38.                             }
  39.                             else {
  40.                                 // store session ID to local storage
  41.                                 localStorage.setItem("user-session", msg.AdditionalData);
  42.                                 console.log("sessionid: " + localStorage.getItem("user-session"));
  43.                             }
  44.  
  45.                             $("buttonSignIn").button('reset');
  46.                             $("#fbLogout").show();
  47.                             $("#linkLogout").hide();
  48.                         });
  49.  
  50.                         request.fail(function (nothing, textStatus, something) {
  51.                             $("#alertDialog").show();
  52.  
  53.                             $("#alertDialog").css("class", "alert alert-danger");
  54.                             $("#alertDialog").html("<p class='glyphicon glyphicon-warning-sign' style='margin-right:10px'/>" + "Something went wrong! :(");
  55.  
  56.                             $("buttonSignIn").button('reset');
  57.                         });
  58.  
  59.                         $("#login-well").hide();
  60.                         $("#userinfo-well").show();
  61.  
  62.                         document.getElementById('fb-log-well').style.display = 'none';
  63.                     })
  64.        
  65. // -------------------- LOGOUT --------------------------
  66.                     document.getElementById('fbLogout').addEventListener('click', function () {
  67.                         var request = $.ajax({
  68.                             url: "http://"+ gameForestIP + ":1193/service/user/login?usersessionid=" + localStorage.getItem("user-session"),
  69.                             async: true,
  70.                             type: "DELETE"
  71.                         });
  72.  
  73.                         request.success(function (data) {
  74.                             if (data.ResponseType != 0) {
  75.                                 // hehe, this is imposible...
  76.                             }
  77.                             else {
  78.                                 FB.logout(function () {
  79.                                     // wipe user
  80.                                     window.location.reload(true);
  81.                                 });
  82.                             }
  83.                         });
  84.                     });
  85. // ---------------- END LOGOUT -----------------------------
  86.                 } else if (response.status === 'not_authorized') {
  87.                     // user is logged in to FB
  88.                     // has not authorized app
  89.  
  90.                     document.getElementById('fb-log-well').style.display = 'block';
  91.                 } else {
  92.                     // user is not logged in to FB
  93.                     // has not authorized app
  94.  
  95.                     document.getElementById('fb-log-well').style.display = 'block';
  96.                 }
  97.             });
  98.  
  99.  
  100.         }
Advertisement
Add Comment
Please, Sign In to add comment