surma
surma

Reputation: 97

Codeigniter with Facebook logout is not working

I made a Facebook login page, but the problem is that the Facebook logout is not working.
I want to log out a user from my application, as well as Facebook when they click on the log out button.
Here is my Facebook login and logout code.
It's been a week now and I can't resolve this issue still.

login

function login(){     

  if ($this->user) {
    try {

      $user_profile = $this->facebook->api('/me');     

      if (!empty($user_profile )) {

        $this->session->unset_userdata("uFname");
        $this->facebook->destroysession();
        $this->session->sess_destroy();  


        $logout_url =$this->facebook->getLogoutUrl(array( 'next' =>base_url().'facebook/logout','access_token'=>$this->facebook->getAccessToken())); 
        $this->session->set_userdata("logoutUrl",$logout_url);


            $this->load->model("login_m");


        $uid=$user_profile['id'];
        $email=((isset($user_profile['email']))?("$user_profile[email]"):(""));
        $dob=((isset($user_profile['dob']))?("$user_profile[dob]"):(""));
        $mbl=((isset($user_profile['mobile']))?("$user_profile[mobile]"):(""));


        $userdata=$this->login_m->checkUser("facebook", $uid, $email);


        if(!empty($userdata)){

          $user_id=$userdata[0]['id'];

          $this->session->set_userdata("uid", $user_id);

          $hash=md5(rand(0, 1000));
          $this->login_m->save($user_id, array(
            "first_name"=>$user_profile['first_name'],
            "last_name"=>$user_profile['last_name'],
            "email"=>$email,
            "dob"=>$dob,
            "mobile"=>$mbl,
            "gender"=>$user_profile['gender'],
            "open_id"=>$uid,
            "oauth_provider"=>"facebook",
            "active"=>$user_profile['verified'],
            "hash"=>$hash
          ));

          header("Location: http://localhost:8888/example/dashboard/");

        }



      }else{
        # For testing purposes, if there was an error, let's kill the script
        die("There was an error.");
      }

    }catch(FacebookApiException $e){
      $this->facebook->destroySession();
      error_log($e);
      $this->user = null;
    }
  }else{

    $loginUrl = $this->facebook->getLoginUrl(array(
      'scope' => 'email,read_stream,offline_access,user_birthday,user_photos, user_relationships,user_about_me'
    ));
    redirect($loginUrl);

  }  

}  

logout

function logout(){
  $this->facebook->setAccessToken('');
  $sessionArray=array(
    "uid"=>'', 
    "uFname"=>'',
    "uLname"=>'',
    "uemail"=>''
  );

    $this->session->unset_userdata($sessionArray);
    $this->session->sess_destroy();
  $this->facebook->destroySession();

  setcookie('PHPSESSID', '', time()-3600, "/");
  setcookie ("fbss_XXXXXXXXXX", "", time()-3600);
  // setcookie ("fbs_205733472943681", "", time() – 3600);
  unset($_SESSION);
  session_destroy();

  header("Location: ".base_url()."auth/showlogin");
} 

Upvotes: 1

Views: 663

Answers (1)

Lloyd_07
Lloyd_07

Reputation: 55

I was also running into this error, a quick fix which i am still testing locally is by creating a logout function at the end of your login controller and destroying the session and redirecting to the login page.

function logout() {
    session_destroy();
    redirect('/login');
} 

let me know if this helps.

thanks

Upvotes: 1

Related Questions