如何在cakephp中使用facebook connect实现注销,同时保留facebook登录?
如何在facebook api中将用户从您的站点注销,同时保持用户使用cakephp登录facebook?(我找到了答案,非常想与大家分享。)我刚刚在阅读后找到了这个答案 基本上,虽然在webtechnick的示例演示中,他将“Facebook.Connect”组件放在AppController中,但如果您想要选择性注销,最好将其放在您想要使用它的实际控制器中。或者将其留在AppController中,并将如何在cakephp中使用facebook connect实现注销,同时保留facebook登录?,facebook,cakephp,Facebook,Cakephp,如何在facebook api中将用户从您的站点注销,同时保持用户使用cakephp登录facebook?(我找到了答案,非常想与大家分享。)我刚刚在阅读后找到了这个答案 基本上,虽然在webtechnick的示例演示中,他将“Facebook.Connect”组件放在AppController中,但如果您想要选择性注销,最好将其放在您想要使用它的实际控制器中。或者将其留在AppController中,并将noAuth=>true传递到Facebook.Connect组件中 无论哪种方式,无论您
noAuth=>true
传递到Facebook.Connect组件中
无论哪种方式,无论您选择哪种方式,您都会设置一个控制器(facebook_controller.php?)来处理facebook登录,并将其组件的noauth设置为false(这是默认值,意味着要进行身份验证[read connect.php以了解这一点])。通过这种方式,您可以完全控制用户何时登录到站点,并且您可以实际将他们注销(使用常规的重定向($this->Auth->logout()
),而无需connect组件在重定向时立即将他们重新登录。下面是一个实现:
让我给你一个想法:
app_controller.php
class AppController extends Controller {
var $components = array('Auth', 'Acl', 'Session');
//or if you want access to "$this->Connect" universally:
// array('Auth', 'Facebook.Connect' =>
// array('noauth'=>'true'), 'Acl', 'Session');
}
用户\u controller.php:
class UsersController extends AppController{
var $helpers = array('Facebook.Facebook');
//an example of the users controller, enabling connect, but
// not authorizing the user (because logout() used by Auth is here)
var $components = array('Email', 'Session', 'Facebook.Connect' => array('createUser'=>false, 'noauth'=>true));
//login() doesnt need to be shown and can be left alone for your traditional users
function logout(){
//if there is no fb user, do the logout normal
if ($this->Connect->FB->getUser() == 0){
$this->redirect($this->Auth->logout());
}else{
//ditch FB data for safety
$this->Connect->FB->destroysession();
//hope its all gone with this
session_destroy();
//logout and redirect to the screen that you usually do.
$this->redirect($this->Auth->logout());
}
}
}
您的“facebook_controller.php”:
类FacebookaController扩展了AppController{
...
//我个人不喜欢让他的作品创造我的用户,因此:
var$components=array('Facebook.Connect'=>array('createUser'=>false));
现在,您的用户/login.ctp文件:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'your app id', // App ID
channelUrl : '//'+window.location.hostname+'/facebook/channel', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
FB.Event.subscribe('auth.statusChange', function(response){
if (response.status == "connected"){
alert('redirecting you to auto facebook login');
//here is out default place for login
window.location.href = "http://"+window.location.hostname + "/facebook/login";
}
});
};
// Load the SDK Asynchronously
(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));
</script>
<?php e($this->Facebook->login(array('registration-url'=>'http://www.yoursite.com/facebook/signup'))); ?>
window.fbAsyninit=函数(){
FB.init({
appId:'您的应用程序id',//应用程序id
channelUrl:'/'+window.location.hostname+'/facebook/channel',//频道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true//解析xfbml
});
//这里有额外的初始化代码
FB.Event.subscribe('auth.statusChange',函数(响应){
如果(response.status==“已连接”){
警报(“将您重定向到自动facebook登录”);
//这里是默认的登录位置
window.location.href=“http://“+window.location.hostname+”/facebook/login”;
}
});
};
//异步加载SDK
(职能(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);
}(文件);
这应该差不多了。我希望这能帮助那些仍然需要帮助的读者
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'your app id', // App ID
channelUrl : '//'+window.location.hostname+'/facebook/channel', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
FB.Event.subscribe('auth.statusChange', function(response){
if (response.status == "connected"){
alert('redirecting you to auto facebook login');
//here is out default place for login
window.location.href = "http://"+window.location.hostname + "/facebook/login";
}
});
};
// Load the SDK Asynchronously
(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));
</script>
<?php e($this->Facebook->login(array('registration-url'=>'http://www.yoursite.com/facebook/signup'))); ?>