简体   繁体   中英

Kohana/PHP Auth fails while issusing a request through AJAX

I have a base class which is inherited by all the controllers. I am having a function in the base class which determines the logged in users role using Auth. Once the users role is determine a variable $LoggedIn_role is set.

This method is correctly called on the initial page load, but later i am issuing ajax calls to check whether the user is still logged in, at that time the Auth::logged_in() always returning 0.

The kohana version i am using is 3.3

Can any one please suggest what is the best approach to circumvent this issue. Thanks.

To login -

if ($ValidObj->check()) {

                    if (Auth::instance()->login($_POST['email'], $_POST['password'],FALSE)) {
                        $this->DoPostLoginJobs();
                    } else {
                        $this->ManageError(Controller_Application::MsgWrongUserCredentials);
                    }
                } else {
                    $this->Form_Errors = $ValidObj->errors('');
                }

To Logout -

public function action_logout() {

        $loggedout = Auth::instance()->logout();
        if ($loggedout)
           HTTP::redirect ('/home/'); // redirects to the home page.
    }

Inside the controller_Application . The base class of all the controllers

 public function DetermineUserRole() {
        $this->LoggedIn_Role = Controller_Application::None;
        try {

            if (Auth::instance()->logged_in('freelancer')) {
                $this->LoggedIn_Role = Controller_Application::Freelancer;
                $this->LoggedIn_Id = Auth::instance()->get_user()->pk();
            } else if (Auth::instance()->logged_in('employer')) {
                $this->LoggedIn_Role = Controller_Application::Employer;
                $this->LoggedIn_Id = Auth::instance()->get_user()->pk();
            }
        } catch (Gettrix_Exception $exc) {
            $this->ManageError(Controller_Application::RedirectNonRecoverableError);
        }



public function before() {
if ($this->request->is_ajax()) {

    $this->auto_render = false;

}


 $this->DetermineUserRole();

if($this->auto_render==TRUE){
parent::before();

   $this->template->content = '';
    $this->template->styles = array();
    $this->template->scripts = array();

View::set_global('site_name', 'TheWebTeam');
View::bind_global('Form_Errors', $this->Form_Errors);
View::bind_global('LoggedIn_Role', $this->LoggedIn_Role);
 View::bind_global('LoggedIn_Id', $this->LoggedIn_Id);
View::bind_global('InvitedEmail', $this->InvitedEmail);
View::bind_global('InvitedUniqueID', $this->InvitedUniqueID);
  View::bind_global('scripts', $this->template->scripts); 
   View::bind_global('styles', $this->template->styles);
}

//This is inside the Home page controller, where it lists all the jobs for the logged in user.

     public function action_joblist()
   {
     echo  Auth::instance()->logged_in() . //The state holds to the initial state, doesn't //change when the user is logged out or logged in.
    }

Please note that action_joblist() is called via AJAX/Jquery call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM