Skip to content

Instantly share code, notes, and snippets.

@ericktai
Last active December 14, 2015 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericktai/2c5be6ce2c626b939ef3 to your computer and use it in GitHub Desktop.
Save ericktai/2c5be6ce2c626b939ef3 to your computer and use it in GitHub Desktop.
gigya.md

loginWithGigya

Introduced: JS SDK Version 0.8.1

Description:

loginWithGigya: function(gigyaUID, gigyaTimestamp, keepLoggedIn, optionsHash) {

facebookAccessToken - the user's Facebook Access Token after the user logs into Facebook with the Facebook JS SDK
keepLoggedIn - This is currently not supported but will be in future versions.  Ignore at this time.
optionsHash (optional) - a hash containing, but not limited to, optional "success" and "error" functions to execute as callbacks

Login a StackMob user with one of their social accounts by Gigya.

Usage:

This code is taken from Gigya social login example and modified for StackMob use.

<!DOCTYPE html>
<!--<html xmlns="http://www.w3.org/1999/xhtml">-->
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
   <title>StackMob Test App</title>
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
   <script type="text/javascript" src="https://s3.amazonaws.com/static.stackmob.com/js/json2-min.js"></script>
   <script type="text/javascript" src="https://s3.amazonaws.com/static.stackmob.com/js/underscore-1.3.3-min.js"></script>
   <script type="text/javascript" src="https://s3.amazonaws.com/static.stackmob.com/js/backbone-0.9.2-min.js"></script>
   <script type="text/javascript" src="https://s3.amazonaws.com/static.stackmob.com/js/2.5.3-crypto-sha1-hmac.js"></script>
   <script type="text/javascript" src="http://static.stackmob.com/js/stackmob-js-0.8.1-bundled-min.js"></script>
   <script type="text/javascript" lang="javascript" 
     src="https://cdn.gigya.com/JS/socialize.js?apikey=your_gigya_api_key">
   </script>

   <script type="text/javascript">
   /* <![CDATA[ */
     StackMob.init({
       appName: 'your_app_name',
       clientSubdomain: 'your_client_subdomain',
       publicKey: 'your_public_key',
       apiVersion: 0
     });

     // onLogin Event handler
     function onLoginHandler(eventObj) { 
       alert(eventObj.context.str + ' ' + eventObj.eventName + ' to ' + eventObj.provider 
         + '!\n' + eventObj.provider + ' user ID: ' +  eventObj.user.identities[eventObj.provider].providerUID);

       // Get the necessary data to call StackMob's `loginWithGigya`
       var gigyaUID = eventObj['UID'];
       var gigyaTimestamp = eventObj['signatureTimestamp'];
       var gigyaSignature = eventObj['UIDSignature'];

       // Time to login using StackMob
       var user = new StackMob.User();
       user.loginWithGigya(gigyaUID, gigyaTimestamp, gigyaSignature, false,  {
         success: function(model, a) {
           console.debug(model);
         },
         error: function(model, response) {
           console.debug(model);
           console.debug(response);
         }
       });  

       // verify the signature ...
       verifyTheSignature(eventObj.UID, eventObj.timestamp, eventObj.signature);

       // Check whether the user is new by searching if eventObj.UID exists in your database
       var newUser = true; // lets assume the user is new
      
       if (newUser) {
         // 1. Register user 
         // 2. Store new user in DB
         // 3. link site account to social network identity
         //   3.1 first construct the linkAccounts parameters
         var dateStr = Math.round(new Date().getTime()/1000.0); // Current time in Unix format
                          //(i.e. the number of seconds since Jan. 1st 1970)

         var siteUID = 'uTtCGqDTEtcZMGL08w'; // siteUID should be taken from the new user record
                                             // you have stored in your DB in the previous step
         var yourSig = createSignature(siteUID, dateStr);

         var params = {
           siteUID: siteUID, 
           timestamp:dateStr,
           cid:'',
           signature:yourSig
         };
               
         //   3.1 call linkAccounts method:
         gigya.socialize.notifyRegistration(params);
       }
  
       document.getElementById('status').style.color = "green";
       document.getElementById('status').innerHTML = "Status: You are now signed in";
     }

     // This method is activated when the page is loaded
     function onLoad() {
       // register for login event
       gigya.socialize.addEventHandlers({
         context: { str: 'congrats on your' }
         , onLogin: onLoginHandler 
         , onLogout: onLogoutHandler
       });
     }

     // Note: the actual signature calculation implementation should be on server side
     function createSignature(UID, timestamp) {
       encodedUID = encodeURIComponent(UID); // encode the UID parameter before sending it to the server.
       // On server side use decodeURIComponent() function to decode an encoded UID
       return '';
     }
    
     // Note: the actual signature calculation implementation should be on server side
     function verifyTheSignature(UID, timestamp, signature) {
       encodedUID = encodeURIComponent(UID); // encode the UID parameter before sending it to the server.
       // On server side use decodeURIComponent() function to decode an encoded UID
       alert('Your UID: ' + UID + '\n timestamp: ' + timestamp + '\n signature: ' + signature + '\n Your UID encoded: ' + encodedUID);
     }
        
     // Logout from Gigya platform. This method is activated when "Logout" button is clicked 
     function logoutFromGS() {
       gigya.services.socialize.logout(); // logout from Gigya platform
     }
    
     // onLogout Event handler
     function onLogoutHandler(eventObj) {
       document.getElementById('status').style.color = "red";
       document.getElementById('status').innerHTML = "Status: You are now signed out";
     }
   /* ]]> */
   </script>
 </head>     
 <body onload="onLoad()">

   <div id="gigya-login-div"></div>

   <div id="status"></div>

   <input id="btnLogout" type="button" value="Sign Out" 
            onclick="logoutFromGS()"/>

   <!-- Best to put this JS before the end of your body tag, i.e. before </body> -->
   <script type="text/javascript">
     gigya.socialize.showLoginUI({
       height: 85
       ,width: 360
       ,showTermsLink:false // remove 'Terms' link
       ,hideGigyaLink:true // remove 'Gigya' link
       ,buttonsStyle: 'fullLogo' // Change the default buttons design to "Full Logos" design
       ,showWhatsThis: true // Pop-up a hint describing the Login Plugin, when the user rolls over the Gigya link.
       ,containerID: 'gigya-login-div' // The component will embed itself inside the loginDiv Div
       ,cid:''
     });
 </script>

</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment