<script>
function initFbLogin() {
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $this->config_model->load_single('fbconnect_app_id'); ?>',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status === 'connected') {
setField();
} else if (response.status === 'not_authorized') {
FB.login();
} else {
FB.login();
}
});
};
(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";
ref.parentNode.insertBefore(js, ref);
} (document));
}
function setField() {
FB.api('/me', function(data) {
//console.log('Good to see you, ' + data.name + '.' + data.id+' '+data.first_name+' '+data.last_name+' '+data.gender+' '+data.email+' '+data.username);
// try to validate account that linked with facebook.
$.ajax({
url: base_url+'account/login/ajax_fb_login',
type: 'POST',
data: csrf_name+'='+csrf_value+'&account_email='+data.email+'&facebook_id='+data.id,
dataType: 'json',
success: function(logindata) {
if (logindata.login_result === true) {
<?php if (isset($go_to)) { ?>
window.location = '<?php echo urldecode($go_to); ?>';
<?php } else { ?>
window.location = base_url;
<?php } ?>
} else {
alert(logindata.login_result_text);
}
}
});
});
}
</script>
<fb:login-button show-faces="false" width="200" max-rows="1" scope="email,user_birthday"></fb:login-button>
如果用户登录到facebook,此代码会使按钮无需登录即可登录 如何在登录前点击它?
答案 0 :(得分:2)
如果您不想自动登录用户,请不要使用FB.Event.subscribe()
。
相反,只需创建一个普通按钮<button></button>
,然后点击该按钮即可使用FB.getLoginstatus()
和FB.login()
执行您想要的操作。因此,点击此按钮即可执行以下操作:
$( "#my_fb_button" ).click(function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
setField();
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
login();
} else {
// the user isn't logged in to Facebook.
login();
}
});
});
function login()
{
FB.login(function(response) {
if (response.authResponse) {
setField();
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email,user_birthday'});
}
希望现在流畅。许多应用程序也使用此流程,它就像魅力一样。祝你好运!
答案 1 :(得分:0)
我在这里遇到了类似的问题,我发现了这篇文章:facebook API how is onlogin called
有一种方法可以将您的javascript函数指定给登录按钮onlogin='your_javascript_function()
,以便添加到按钮中。