1

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

Cakephp 和 facebook-sdk 是最新版本。这是我的帐户控制器:

    <?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

4 回答 4

1

我的应用控制器

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'
    ));
于 2013-01-09T09:54:20.947 回答
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

于 2013-01-09T14:21:55.823 回答
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'];因为我认为在提供加密密码登录时无法进行身份验证。

于 2013-01-09T08:16:53.280 回答
0

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

于 2013-01-11T21:14:22.357 回答