作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关于将 Facebook 登录与在线 cakephp Auth 组件集成的最新资源似乎很少甚至没有。我找到了以下资源:
最佳答案
Cakephp 2.x Auth 与 Facebook Auth 的集成以实现无缝用户身份验证
首先,您应该阅读奇妙的 cakePHP Auth Component并关注 Simple Authentication and Authorization Application tutorial来自 cakephp book 2.x(假设您还学习了本系列的前两个教程。完成后,您应该已经成功构建了一个具有用户身份验证和授权的简单 cakePHP 应用程序。
接下来你应该下载 facebook SDK并获得 App ID来自 Facebook 。
首先,我们将 Facebook sdk 复制到 App/Vendors。然后我们将在 AppController beforeFilter 中导入并初始化它方法。
//app/Controller/AppController.php
public function beforeFilter() {
App::import('Vendor', 'facebook-php-sdk-master/src/facebook');
$this->Facebook = new Facebook(array(
'appId' => 'App_ID_of_facebook',
'secret' => 'App_Secret'
));
$this->Auth->allow('index', 'view');
}
//app/Controller/AppController.php
public function beforeRender() {
$this->set('fb_login_url', $this->Facebook->getLoginUrl(array('redirect_uri' => Router::url(array('controller' => 'users', 'action' => 'login'), true))));
$this->set('user', $this->Auth->user());
}
redirect_uri
到我们的应用程序用户/登录操作。这样一旦 facebook 对用户进行了身份验证,我们也可以使用 cake::Auth 登录他。这样做有很多好处,
including the solution for this question .
<!-- App/Views/Layouts/default.ctp just after <div id="content"> -->
<?php
if($user) echo 'Welcome ' . $user['username'];
else {
echo $this->Html->link('Facebook Login', $fb_login_url) . ' | ';
echo $this->Html->link('Logout', array('controller' => 'user', 'action' => 'logout'));
?>
// App/Controller/UsersController.php
// Handles login attempts from both facebook SDK and local
public function login()
{
// If it is a post request we can assume this is a local login request
if ($this->request->isPost()){
if ($this->Auth->login()){
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Invalid Username or password. Try again.'));
}
}
// When facebook login is used, facebook always returns $_GET['code'].
elseif($this->request->query('code')){
// User login successful
$fb_user = $this->Facebook->getUser(); # Returns facebook user_id
if ($fb_user){
$fb_user = $this->Facebook->api('/me'); # Returns user information
// We will varify if a local user exists first
$local_user = $this->User->find('first', array(
'conditions' => array('username' => $fb_user['email'])
));
// If exists, we will log them in
if ($local_user){
$this->Auth->login($local_user['User']); # Manual Login
$this->redirect($this->Auth->redirectUrl());
}
// Otherwise we ll add a new user (Registration)
else {
$data['User'] = array(
'username' => $fb_user['email'], # Normally Unique
'password' => AuthComponent::password(uniqid(md5(mt_rand()))), # Set random password
'role' => 'author'
);
// You should change this part to include data validation
$this->User->save($data, array('validate' => false));
// After registration we will redirect them back here so they will be logged in
$this->redirect(Router::url('/users/login?code=true', true));
}
}
else{
// User login failed..
}
}
}
关于facebook-graph-api - 如何将 Facebook SDK 登录与 cakephp 2.x 集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18037971/
我是一名优秀的程序员,十分优秀!