logeeks
logeeks

Reputation: 4979

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

Upvotes: 2

Views: 128

Answers (3)

logeeks
logeeks

Reputation: 4979

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.

Upvotes: 0

mrBrown
mrBrown

Reputation: 153

Is your AJAX call requested using a browser? (And not a CURL request). If so, check if the AJAX call succeeds, and what it returns (use e.g. the firefox firebug console tab)

It seems to me you don't have to worry about credentials, since you mention it's on the same domain.So the check if the user is logged in done by the php controller at the AJAX endpoint.

So with a normal AJAX call you should be able to call your controller where you have the check if someone is logged in. E.g.

// set ajax setup
$.ajaxSetup({
type : "GET",
statusCode : {
    404 : function() {
        alert('Page not found.');
    },
    500 : function() {
        alert('Server error.');
    }
}

});

$.ajax({
    dataType : "json",
    url : 'some/uri/',
    data : {
        name : name
    },
    success : function(response) {
       // do something with the response
    }
});

Does this help, or did I just completely missed your point?

Upvotes: 2

Raja Sekar
Raja Sekar

Reputation: 2130

Use data type : jsonp.

Hope this will solve your problem.

Upvotes: 2

Related Questions