-
-
Save ghankerson/11233819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($, D, W, undefined){ | |
// Module pattern see http://learn.jquery.com/code-organization/concepts/ | |
var dapp = (function (){ | |
var config= { | |
dataRef : new Firebase("https://boiling-fire-4861.firebaseio.com/"), | |
childDataRef : new Firebase("https://boiling-fire-4861.firebaseio.com/" + "loggedUsers"), | |
disqusdiv : "disqus_thread", | |
sociallogin: "#sociallogin", | |
logout: '.logout', | |
DISQUS_SECRET : "eMN5rithDmeqq20bqM0QbTXHUsAhoCy5HhQ2rngHC6pkXwYBL23xMrTsBBVvlIta", | |
DISQUS_PUBLIC : "cIjf1xtcOj6S9LONsiaV5o38WMEsWQtanZZpJzzKrDor42lxufhp8cGmk3zGIWos", | |
discus_shortname: 'marketplacetest', | |
}; | |
var init = function () { | |
$(config.sociallogin).bind('click', login); | |
$(config.logout).bind('click', logout); | |
$(config.logout).hide(); | |
}; | |
var logUserInfo= function (user) { | |
var stamp = new Date(); | |
var email = typeof(user.thirdPartyUserData.email) === 'string' ? user.thirdPartyUserData.email : 'N/A'; | |
config.childDataRef.child(user.displayName).set({ | |
lastvisittimestamp: stamp.toUTCString(), | |
name: user.displayName, | |
provider: user.provider, | |
email: email, | |
lastvisitpage: window.location.href | |
}); | |
}; | |
var auth = new FirebaseSimpleLogin(config.dataRef, function(error, user) { | |
var dconfig = {}; | |
if (error) { | |
// an error occurred while attempting login | |
console.log(error); | |
} else if (user) { | |
// user authenticated with Firebase | |
$("<div id='user_info'>You are logged in as " + user.displayName + " via " + user.provider + " </div>").appendTo("#comments"); | |
logUserInfo(user); | |
$(config.logout).show(); | |
$(config.sociallogin).hide(); | |
dconfig = disqusSignon(user); | |
} else { | |
// user is logged out | |
$("#user_info").hide().remove(); | |
$(config.logout).hide(); | |
$(config.sociallogin).show(); | |
} | |
loadDisqus(dconfig); | |
}); | |
var login = function(e) { | |
e.preventDefault(); | |
var provider = e.target.className.replace(/sociallogin\s/, ''); | |
if(provider === 'facebook') { | |
auth.login(provider, { | |
rememberMe: true, | |
scope: 'email' | |
}); | |
} | |
else { | |
auth.login(provider); | |
} | |
}; | |
var logout= function(e) { | |
e.preventDefault(); | |
auth.logout(); | |
}; | |
var loadDisqus = function(hsh) { | |
console.log(hsh.auth); | |
window.disqus_config = (function () { | |
this.page.remote_auth_s3 = hsh.auth; | |
this.page.api_key = config.DISQUS_PUBLIC; | |
}()); | |
var disqus_shortname = config.disqus_shortname; | |
var disqus_identifier = $('title').text(); | |
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; | |
dsq.src = '//' + config.disqus_shortname + '.disqus.com/embed.js'; | |
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); | |
}; | |
var utf8_to_b64= function ( str ) { | |
return window.btoa(encodeURIComponent( escape( str ))); | |
}; | |
var disqusSignon= function (user) { | |
var disqusData = { | |
id: user.id, | |
username: user.displayName, | |
email: user.email | |
}; | |
var disqusStr = JSON.stringify(disqusData); | |
var timestamp = Math.round(+new Date() / 1000); | |
var message = utf8_to_b64(disqusStr); | |
/* | |
* CryptoJS is required for hashing (included in dir) | |
* https://code.google.com/p/crypto-js/ | |
*/ | |
var result = CryptoJS.HmacSHA1(message + " " + timestamp, config.DISQUS_SECRET); | |
var hexsig = CryptoJS.enc.Hex.stringify(result); | |
return { | |
auth: message + " " + hexsig + " " + timestamp | |
}; | |
}; | |
// public api | |
return { | |
init: init | |
}; | |
})(); | |
D.behaviors.firebase = { | |
attach: function (context, settings) { | |
dapp.init(); | |
} | |
}; | |
}(jQuery, Drupal, Window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment