CakePHP + Facebook

时光毁灭记忆、已成空白 提交于 2019-12-03 10:09:28
deceze

Apparently I'm doing the same thing a little before you... ;-)

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);
}

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'));
}

Look no further. Here is an excellent article that'll guide you all the way through (minus any readymade plugins):
Integrating Facebook Connect with CakePHP's Auth component

Simply follow the approach described in there.

Cheers, m^e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!