0

Hi Guys first of all let me tell you guys that I am a newbie with CakePHP and MVC also but one of my old client requested me some favor which I took as a challenge. So If anyone can help me I will appreciate a lot.

I have a CakePHP 2.9 application where OAuth is not working properly following is the code for app/Controller/UsersController.php

 public function facebookLogin(){

    $this->layout = false;

    App::import('Vendor', 'Facebook/facebook');
    /*$facebook = new Facebook(array(
                                  'appId'  => '1867587306812489',   // Facebook App ID 
                                  'secret' => '9fb41a07d3da3406dbab5861c3498764',  // Facebook App Secret
                                  'cookie' => false,    
                                  )
                            );   */ 
    $facebook = new Facebook(array(
                                  'appId'  => '1887387108164606',   // Facebook App ID 
                                  'secret' => '8d733aa6a4501ce27fde9626429baab9',  // Facebook App Secret
                                  'cookie' => false,    
                                  )
                            );  
    $user = $facebook->getUser();
    if ($user) {
         try {
                $user_profile = $facebook->api('/me?fields=first_name,last_name,email');

        $usersArr = $this->User->find('first',array('conditions'=>array('User.email'=>$user_profile['email'],'User.social_id'=>$user_profile['id'],'User.role_id'=>0)));


                                      $user_count = $this->User->find('first',array('conditions'=>array(),'order'=>'User.id DESC','limit'=>1));

                if(empty($usersArr)){

                        $saveUserArr['User']['username'] =  trim($user_profile['first_name']).' '.trim($user_profile['last_name']);
                        $saveUserArr['User']['firstname'] =  $user_profile['first_name'];

                        $saveUserArr['User']['lastname'] =   $user_profile['last_name'];

                                                   /* $user_n = $saveUserArr['User']['first_name'].'.'.$saveUserArr['User']['last_name'];
                        $user_cnt = $user_count['User']['id'] + 1;
                        $user_n = str_replace(' ','_',$user_n).'.'.$user_cnt;
                        $saveUserArr['User']['user_unique_id'] = $user_n;  */

                        $saveUserArr['User']['email'] =      $user_profile['email'];

                        $saveUserArr['User']['password'] =   '123456';

                        $saveUserArr['User']['social_id'] =  $user_profile['id'];

                        $saveUserArr['User']['login_from'] =  'facebook';

                        $saveUserArr['User']['role_id'] =  0;

                        $this->User->save($saveUserArr['User'],false);

                }else{

                    $this->User->id = $usersArr['User']['id'];

                    $this->User->saveField('social_id', $user_profile['id']);

                    $this->User->saveField('login_from', 'facebook');

                }

            $this->request->data['User']['email'] =  $user_profile['email'];

            $this->request->data['User']['login_from'] =  'facebook';       

          } catch (FacebookApiException $e) {

             error_log($e);

             $user = null;

          }

    } else {

     $loginUrl = $facebook->getLoginUrl(array(

            'scope'     => 'email', // Permissions to request from the user

            ));

     return $this->redirect($loginUrl);
    }                       
}

private function deleteFacebookSession(){

    Configure::write('debug', 0);   

    if(!$this->Session->check('Auth.User')){

        foreach( $this->Session->read() as $key => $value ) {

            if( strpos( $key, 'fb_' ) === 0 ) {

                $this->Session->delete($key);

            }
        }
    }
}

It throws following error: enter image description here

Bilal Naseer
  • 156
  • 1
  • 2
  • 16

1 Answers1

0

The oauth script url is not properly configured in your fb app. You may go to your fb app settings and look for APP DOMAIN.

Include your domain there:

facebook app domain input

If you are developing in your localhost, you may want to create a server.local rec in your hosts file so you can access your localserver as server.local.

Also, you can create test versions of the main fb app, and allows you to have a copy of your fb app that works in other environments, it is called "create test app"

Facebook create test app dialog

Gestudio Cloud
  • 290
  • 2
  • 12