Created
September 17, 2011 13:00
-
-
Save alexmic/1223918 to your computer and use it in GitHub Desktop.
FB Login
This file contains 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
/** | |
* @fileOverview Signup page JS code. | |
* @author <a href="mailto:[email protected]"> Alex Michael </a> | |
* @requires PK.js, facebox.js | |
*/ | |
PK.onPageLoad(function(){ | |
// Once the register page is loaded, make | |
// a request to FB and fill in user fields. | |
/** | |
* @ignore | |
*/ | |
$.facebox.settings.modal = true; | |
$.facebox.settings.showCloseBtn = false; | |
$.facebox("<div style='text-align:center'><div>Please wait while Songjam connects to Facebook.</div><br> " + | |
"<img src='/static/imgs/bar-load.gif' /></div>"); | |
FB.getLoginStatus(function(response) { | |
if (response.session) { | |
setFBUser(response.session); | |
} else { | |
FB.login(function(response) { | |
if (response.session) { | |
setFBUser(response.session); | |
} else { | |
// user cancelled login | |
} | |
}); | |
} | |
}); | |
/** | |
* Once the user is successfully logged in, this method | |
* sets the user to the PKFB object and then makes a | |
* request to the FB Graph to retrieve information for | |
* them. | |
* | |
* @param {Object} session The FB session returned from the FB.getLoginStatus() call. | |
*/ | |
function setFBUser(session) | |
{ | |
PK.fb.setUser(session); | |
FB.api("/me", function(response){ | |
console.log(response); | |
fillFields(response); | |
$.facebox.close(); | |
}); | |
} | |
/** | |
* Helper methods to fill in the signup form with info from Facebook. | |
* | |
* @param {Object} fbResponse The Facebook response. | |
*/ | |
function fillFields(fbResponse) | |
{ | |
$("#name").val(fbResponse.first_name); | |
$("#surname").val(fbResponse.last_name); | |
} | |
/** | |
* Signs up the user. Used as a callback to click and keyup events in the signup form. | |
*/ | |
function signup() | |
{ | |
var email = $("#email").val(); | |
var pass = $("#password").val(); | |
var name = $("#name").val(); | |
var surname = $("#surname").val(); | |
if (email !== "" && pass !== "" && name !== "" && surname != "") { | |
$("#signup-btn").html("Creating your account.."); | |
PK.rpc.user.Ops.create_user({email: email, password:pass, name:name, surname:surname, fbid:PK.fb.userID}, | |
function(response) | |
{ | |
if (!response.s) { | |
PK.putError(response.err); | |
$("#signup-btn").html("Signup"); | |
} else { | |
window.location = response.r_url; | |
} | |
}); | |
} | |
} | |
PK.dom.bind({ | |
"#email #password #name #surname keyup" : function(target, event) | |
{ | |
if (event.keyCode === 13) { | |
signup(target); | |
} | |
}, | |
"#signup-btn click" : function(target) | |
{ | |
signup(target); | |
}, | |
"#email blur" : function(target) | |
{ | |
var email = target.val(); | |
if (email) { | |
PK.rpc.user.Ops.email_exists({email: email}, function(response) { | |
if (response.s) { | |
PK.putError("This email address already exists."); | |
} else { | |
if ($("#msg-container").is(":visible")) { | |
PK.putOK("Okay, this one's fine.") | |
}; | |
} | |
}); | |
} | |
} | |
}, true); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment