在facebook认证后,cakephp没有登录

时间:2013-01-08 19:12:24

标签: facebook-php-sdk cakephp-2.1

当我使用facebook-sdk创建用户时,我不会让他在登录facebook后自动登录。 Usercreation运行良好,但没有登录。

Cakephp和facebook-sdk是最新版本。 这是我的accountController:

    <?php

class AccountController extends AppController {

    public $uses = array('User');

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('login');
        #$this->Auth->authenticate = array('Basic' => array('fields' =>array('username' => 'fb_id', 'password' => 'random')));

    }

    public function login() {
        $this->facebook = new Facebook(array(
            'appId' => '14549077xxxx',
            'secret' => '95a38f3xxxx',
            'cookie' => 'true'
        ));

        $user = $this->facebook->getUser();
        if($user){
            try{
                $params = array('next' => 'http://'.$_SERVER['HTTP_HOST'].'/account/logout');
                $this->Session->write('logoutLink', $this->facebook->getLogoutUrl($params));

                #if (!$this->Auth->user()) {
                    $fb_user = $this->facebook->api('/me');

                    $authUser = $this->User->findByFbId($user);

                    if (empty($authUser)) {
                        $pw = $this->__randomString();
                        $authUser = array(
                            'fb_id' => $user,
                            'random' => $pw,
                            'password' => $this->Auth->password($pw),
                            'email' => $fb_user['email'] 

                        );
                        $this->User->create();
                        $this->User->save($authUser);
                        $authUser = $this->User->findByFbId($user);
                    }

                    $this->Auth->authenticate = array(
                        'Form' => array(
                                'fields' => array('username' => 'fb_id', 'password' => 'random')
                        )
                    );

                    $this->Auth->login($authUser['User']);

                    $this->redirect($this->Auth->redirect());
                #}
            }catch(FacebookApiException $e){
                $user = NULL;
            }
        }
        if(empty($user)){
            $loginurl = $this->facebook->getLoginUrl(array(
                'scope'=> 'email,publish_stream,read_friendlists',
                'redirect_uri'  => 'http://'.$_SERVER['HTTP_HOST'].'/account/login',
                ));

            $this->redirect($loginurl);
        }
    }

    public function dashboard() {
        echo 'logged in';
    }
}

用户将始终重定向到登录页面。 $ authUser始终正确填充。

请帮助:)

问候 米。

4 个答案:

答案 0 :(得分:1)

我的AppController

class AppController extends Controller {

    public $components = array('Auth','Session');

    public function beforeFilter() {
        $this->Auth->loginRedirect = array('controller' => 'account', 'action' => 'dashboard');
        $this->Auth->logoutRedirect = '/';
        $this->Auth->loginAction = array('controller' => 'pages', 'action' => 'login');
    }

会话:

Configure::write('Session', array(
        'defaults' => 'php'
    ));

答案 1 :(得分:1)

聊天问题解决了:

Facebook正在创建一个新会话,

$this->facebook = new Facebook(array(
            'appId' => '14549077xxxx',
            'secret' => '95a38f3xxxx',
            'cookie' => 'true'
        ));

此代码必须在appcontroller中写下

public function beforeFilter() {
        $this->Auth->loginRedirect = array('controller' => 'account', 'action' => 'dashboard');
        $this->Auth->logoutRedirect = '/';
        $this->Auth->loginAction = array('controller' => 'pages', 'action' => 'login');
        $this->facebook = new Facebook(array(
            'appId' => '14549077xxx',
            'secret' => '95a38f3xxx',
            'cookie' => 'true'
        ));

    }

非常感谢@noslone一起尝试:D

答案 2 :(得分:0)

提供用于登录用户的数据$this->Auth->login($authUser['User']);是错误的。

根据CakePHP文档:

In 2.0 $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful.

你应该这样做:

$this->request->data['User'] = $authUser['User'];
$this->request->data['User']['password'] = $authUser['User']['random'];

$this->Auth->login();

我还添加了$this->request->data['User']['password'] = $authUser['User']['random'];,因为我认为在提供加密密码登录时无法进行身份验证。

答案 3 :(得分:0)

如果您有任何其他问题,我正在开发一个使用Facebook oAuth作为Auth Component的身份验证对象的插件。如果您想要一个使用服务器端Facebook登录的已经构建的解决方案,请查看我的网站:http://marianofino.github.com/Facebook-Plugin-for-CakePHP/