Search code examples
javascriptjqueryajaxkohana-3.3kohana-auth

Kohana/JQuery Auth access issue


I have a website using the Kohana ORM Auth. I am having a url which when called via ajax will check whether the user is logged in and return json_encoded data corresponding to the users role. this url is working fine when i tried it in normal browser window. It was returning correct values, but when tried via AJAX calls the Auth::instance()->logged_in() is returning FALSE even i am sure that the user is logged in. I am following the instructions laid out in the following link jQuery getJSON doesnt send cookies but it didn't fix the issue.

What I have done so far is that , I added the following lines in the base controller as well as in the file where the request action is written

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');  
header('Access-Control-Allow-Credentials: TRUE'); 

In the ajax before sending the actual request the following code written

beforeSend: function(xhr){
            xhr.withCredentials = true;
        },

EDIT--------------

I tried using the following but it gave an COR issue for me.

$.ajax({
        type: 'GET',
        url: "http://www.domain.com/web/joblist",
        xhrFields: {
            withCredentials: true
        },
        dataType: "json",
        data:{
            "tab":currentTab,
            "page":currentPage,
            "filter":filterStr
        },
        success: function(json){  ...
        },
        error: function(e) {
            ...
        }
    });

Solution

  • The problem was that www. not being added to the beginning of the url and auth was denying to service request from the url which dont have www. prepended to it.