CakePHP + Facebook [英] CakePHP + Facebook
问题描述
我想实现facebook连接到我的cakephp应用程序。我使用Nick的Facebook插件。
I am trying to implement facebook Connect to my cakephp Application. i am using Nick's Facebook Plugin.
我想用这种方式实现
- 当用户访问网站时,他应该可以通过网站上的注册或Facebook Connect登录
- 现有用户应该能够将他们的帐户连接到他们的FB帐户
- 首次使用FB Connect登录该网站的人员,并且在网站上没有帐户。应重定向到他们必须输入详细信息以完成配置文件的页面。
我做了什么 -
遵循尼克的指令实现它,当我单击登录 - 它连接到我的应用程序。但我不明白如何创建与Fb Connect Id相关联的用户名和密码。
What i have done - I have followed the instruction of Nick to implement it and when i click Login - it connects to my app. but i dont understand how to create a username and password associated with the Fb Connect Id. and user it against the FB token.
推荐答案
这里有一个Facebook登录的方法我正在使用(略微编辑和注释):
Here's a method for Facebook login I'm using (slightly redacted and annotated):
public function facebook($authorize = null) {
App::import('Lib', 'Facebook.FB');
$Fb = new FB();
$session = $Fb->getSession();
// not logged into Facebook and not a callback either,
// sending user over to Facebook to log in
if (!$session && !$authorize) {
$params = array(
'req_perms' => /* the permissions you require */,
'next' => Router::url(array('action' => 'facebook', 'authorize'), true),
'cancel_url' => Router::url(array('action' => 'login'), true)
);
$this->redirect($Fb->getLoginUrl($params));
}
// user is coming back from Facebook login,
// assume we have a valid Facebook session
$userInfo = $Fb->api('/me');
if (!$userInfo) {
// nope, login failed or something went wrong, aborting
$this->Session->setFlash('Facebook login failed');
$this->redirect(array('action' => 'login'));
}
$user = array(
'User' => array(
'firstname' => $userInfo['first_name'],
'lastname' => $userInfo['last_name'],
'username' => trim(parse_url($userInfo['link'], PHP_URL_PATH), '/'),
'email' => $userInfo['email'],
'email_validated' => $userInfo['verified']
),
'Oauth' => array(
'provider' => 'facebook',
'provider_uid' => $userInfo['id']
)
);
$this->oauthLogin($user);
}
这给了我一个包含所有用户信息的数组,调用 :: oauthLogin
,它使用给定信息记录用户或要求用户填写缺少的详细信息和/或在数据库中创建新的用户记录。从Facebook API获得的最重要的部分是 $ userInfo ['id']
和/或电子邮件地址,您可以使用它们来识别您的数据库。如果您使用AuthComponent,可以使用 $ this-> Auth-> login($ user_id)
手动登录用户,其中 $ user_id
是您自己数据库中的用户的ID。
This gives me an array with all the user details I could grab from Facebook and invokes ::oauthLogin
, which either logs the user in with the given information or asks the user to fill in missing details and/or creates a new user record in the database. The most important part you get from the Facebook API is the $userInfo['id']
and/or email address, either of which you can use to identify the user in your database. If you're using the AuthComponent, you can "manually" log in the user using $this->Auth->login($user_id)
, where $user_id
is the id of the user in your own database.
private function oauthLogin($data) {
$this->User->create();
// do we already know about these credentials?
$oauth = $this->User->Oauth->find('first', array('conditions' => $data['Oauth']));
if ($oauth) {
// yes we do, let's try to log this user in
if (empty($oauth['User']['id']) || !$this->Auth->login($oauth['User']['id'])) {
$this->Session->setFlash('Login failed');
}
$this->redirect('/');
}
// no we don't, let's see if we know this email address already
if (!empty($data['User']['email'])) {
$user = $this->User->find('first', array('conditions' => array('email' => $data['User']['email'])));
if ($user) {
// yes we do! let's store all data in the session
// and ask the user to associate his accounts
$data['User'] = array_merge($data['User'], $user['User']);
$data['Oauth']['user_id'] = $user['User']['id'];
$this->Session->write('Oauth.associate_accounts', $data);
$this->redirect(array('action' => 'oauth_associate_accounts'));
}
}
// no, this is a new user, let's ask him to register
$this->Session->write('Oauth.register', $data);
$this->redirect(array('action' => 'oauth_register'));
}
这篇关于CakePHP + Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!