Last active
December 16, 2015 05:39
-
-
Save kennycason/5386134 to your computer and use it in GitHub Desktop.
Facebook Login/Signup Example - with account merging
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
define('FB_APP_ID', '375664735854032'); | |
define('FB_APP_SECRET', '7fe05d4179b32e5925a96f85c777fa64'); |
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
// require_once(LIB_DIR . 'social/facebook/facebook.php'); | |
... | |
// get logged in fb user | |
$facebook = new Facebook(array( | |
'appId' => FB_APP_ID, | |
'secret' => FB_APP_SECRET, | |
)); | |
// get logged in fb user id | |
$userId = $facebook->getUser(); | |
// get array of user info | |
$user = $facebook->api('/me'); |
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
var LIB = LIB || {}; | |
LIB.Facebook = {}; | |
LIB.Facebook.appId = 0; | |
LIB.Facebook.permissions = 'email'; | |
LIB.Facebook.jsLoaded = false; | |
LIB.Facebook.load = function(appId, locale) { | |
LIB.Facebook.appId = appId; | |
if(locale == null) { | |
locale = 'en_US'; | |
} | |
window.fbAsyncInit = function() { | |
LIB.Facebook.init(); | |
}; | |
(function(d){ | |
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; | |
if (d.getElementById(id)) { | |
return; | |
} | |
js = d.createElement('script'); | |
js.id = id; | |
js.async = true; | |
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=" + LIB.Facebook.appId; | |
ref.parentNode.insertBefore(js, ref); | |
}(document)); | |
} | |
LIB.Facebook.init = function() { | |
FB.init({ | |
appId : LIB.Facebook.appId , | |
status : true, | |
cookie : true, | |
xfbml : true, | |
oauth : true | |
}); | |
FB.Event.subscribe('auth.login', function(response) { | |
LIB.Facebook.onlogin(); | |
// alert("FB login"); | |
// window.location.reload(); | |
}); | |
FB.Event.subscribe('auth.logout', function(response) { | |
// alert("FB logout"); | |
// window.location.reload(); | |
}); | |
}; | |
/** | |
* | |
* response: | |
* authResponse: | |
* accessToken | |
* userID | |
* expiresIn | |
* signedRequest | |
* status | |
*/ | |
LIB.Facebook.login = function(success, fail) { | |
FB.login(function(response) { | |
if(response.status == 'connected') { | |
if(success) { | |
success(); | |
} | |
//window.location = redirect; | |
//LIB.Facebook.onlogin(redirect); | |
return true; | |
} else { | |
if(fail) { | |
fail(); | |
} | |
return false; | |
} | |
}, { | |
scope : LIB.Facebook.permissions | |
} | |
); | |
} | |
LIB.Facebook.onlogout = function() { | |
window.location = LIB.BASE_URL + 'user/logout'; | |
} | |
LIB.Facebook.onlogin = function() { | |
$.post(LIB.BASE_URL + "json/user/fb/login", function(json) { | |
//alert(json.status + " msg: " + json.msg + " data:" + json.data); | |
if(json.status == 'OK') { | |
if(redirect == null ) { | |
redirect = LIB.BASE_URL; | |
} | |
window.location = redirect; | |
} else { | |
$("#login_error_msg").html(json.msg); | |
} | |
},"json"); | |
} | |
/* | |
LIB.Facebook.autoLogin = function() { | |
// alert("ON LOGIN"); | |
$.post(LIB.BASE_URL + "json/user/fb/login", function(json) { | |
// alert(json.status + " msg: " + json.msg + " data:" + json.data); | |
if(json.status == 'OK') { | |
window.location.reload() | |
} | |
},"json"); | |
} | |
*/ |
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
// add these function in User.php | |
public function fbLogin() { | |
// See if there is a user from a cookie | |
$user = $this->facebook->getUser(); | |
if ($user) { | |
try { | |
$profile = $this->facebook->api('/me'); | |
$email = $profile['email']; | |
$facebook_id = $profile['id']; | |
$djname = "DJ " . $profile['first_name'] . " " . $profile['last_name']; | |
if ($this->fbIdUnique($facebook_id)) { // new facebook user, handle that | |
// return fail('Facebook Signup disabled for now'); | |
// Proceed knowing you have a logged in user who's authenticated. | |
if ($this->emailUnique($email)) { // completely new user | |
//$email, $password, $password_confirm, $tos, $djname, $facebook_id = 0, $twitter_id = 0 | |
$ret = $this->signup($email, null, null, 1, $djname, $facebook_id, 0); | |
if ($ret['status'] == 'OK') { // successfully created a new user via FB, now log in | |
return $this->fbLogin(); | |
} else { | |
return fail('Succesfully connected to Facebook, Error occured while creating Streamix account'); | |
} | |
} else { // user with this email exists, so merge accounts | |
$ret = $this->update(array('facebook_id'))->where('email = :email')->execute(array('facebook_id' => $facebook_id, 'email' => $email)); | |
if ($ret['status'] == 'OK') { // successfully merged accounts, now log in | |
return $this->fbLogin(); | |
} else { | |
return fail('Succesfully connected to Facebook, Error occured while merging existing Streamix account with Facebook account'); | |
} | |
} | |
} else { // existing facebook user so, just log in | |
Session::set('facebook_id', $facebook_id); | |
$ret = $this->select()->where(" facebook_id = :facebook_id ")->execute(array('facebook_id' => $facebook_id)); | |
if ($ret['status'] == 'OK' && count($ret['data']) == 1) { | |
$this->info['info'] = $ret['data'][0]; | |
} else { | |
return fail('An unexpected error occured when logging in via Facebook'); | |
} | |
$this->loginSuccess(); | |
return success('Logged in!'); | |
} | |
} catch (FacebookApiException $e) { | |
return fail('Error occured in Facebook API, please try again later.'); | |
} | |
} else { | |
return fail('You do not appear to be logged into Facebook'); | |
} | |
return fail('You do not appear to be logged into Facebook'); | |
} | |
public function fbIdUnique($facebook_id) { | |
$info = $this->select(array('id'))->where("facebook_id = :facebook_id")->execute(array('facebook_id' => $facebook_id)); | |
if ($info['status'] == 'OK') { | |
if (count($info['data'])) { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
return true; | |
} | |
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
// have this javascript on the signup/signin page, modify as needed | |
// in header | |
LIB.Facebook.load('<?php echo FB_APP_ID; ?>', '<?php echo Session::get('locale'); ?>'); | |
$(document).ready(function() { | |
$("#login_button").click(function() { | |
LIB.Facebook.login(function() { | |
$("#login").submit(); | |
}, | |
function() { | |
alert("facebook login failed"); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment