Kohana / PHP Auth在通过AJAX发出请求时失败

时间:2014-08-18 17:47:51

标签: php ajax kohana kohana-3.3

我有一个由所有控制器继承的基类。我在基类中有一个函数,它使用Auth确定登录用户角色。一旦用户角色确定了一个变量$ LoggedIn_role被设置。

在初始页面加载时正确调用此方法,但稍后我发出ajax调用以检查用户是否仍然登录,此时Auth :: logged_in()始终返回0。

我使用的kohana版本是3.3

任何人都可以建议绕过这个问题的最佳方法是什么。感谢。

登录 -

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

退出 -

public function action_logout() {

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

在controller_Application中。所有控制器的基类

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

//这是在主页控制器中,它列出了登录用户的所有作业。

     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.
    }

请注意,action_joblist()是通过AJAX / Jquery调用调用的。

1 个答案:

答案 0 :(得分:0)

相关问题