CakePHP + Facebook

后端 未结 2 2078
耶瑟儿~
耶瑟儿~ 2021-02-11 10:02

I am trying to implement facebook Connect to my cakephp Application. i am using Nick\'s Facebook Plugin.

I wanna implement it this way

  1. When a user Visits t
2条回答
  •  情深已故
    2021-02-11 10:35

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

提交回复
热议问题