Cannot detect if User is Logged in when using Paginator (backbone.js)
-
12-12-2019 - |
Vra
I am using the Backbone.js Paginator plugin. It does infinite scroll pagination well by defining its own Collection for the models involved in the pagination. If the user is logged in, the PHP backend will return the Backbone object with an additional attribute is_liked
to cause that item to have a different CSS styling.
Problem: When the Paginator plugin (with its own Collection Backbone.Paginator.requestPager
) does a GET fetch
request on the backend, the backend is not able to determine if the user is logged in! However, when I use the usual Backbone.Collection
, the backend is able to determine whether the user is logged in! It works with a $.post()
too. This makes me think the problem lies in the plugin's Backbone.Paginator.requestPager
I am checking the results by using the Network section of Chrome's developer tools. If the authentication check works, api/test
can be seen to return some data about the user. If login status cannot be determined, it returns null
UPDATE: The GET
request sent by Backbone.Paginator.requestPager
collection does not include the Cookie
info in the headers that is found in the GET
request sent by Backbone.Collection
. Could this be the problem? How can I force Backbone.Paginator.requestPager
to not strip out the cookie data from the headers?
What is happening here? And how can I solve this (without rewriting my own pagination code)?
Auth Test using PHP Backend (Laravel Framework)
Route::any('api/test', function() {
// This will return some data of the user if logged in
return json_encode(Auth::user());
});
Typical Backbone Collection [Works]
SimilarUserCollection = Backbone.Collection.extend({
model: User,
url: 'api/test'
});
Paginator's Collection [Doesnt Work]
PhotoCollection = Backbone.Paginator.requestPager.extend({
model: Photo,
paginator_core: {
type: 'GET',
dataType: 'json',
url: 'api/test'
},
paginator_ui: {
firstPage: 1,
currentPage: 1,
perPage: 7,
totalPages: 10
},
server_api: {
'page': function() { return this.currentPage; }
},
parse: function (response) {
return response;
}
});
Oplossing
This is a crossdomain issue:
One Request is to this URL: http://www.mysite.com/api/test?page=1
And the other to this: http://mysite.com/api/test
For the browsers www.mysite.com is totally different so if the Cookie is generated on www.mysite.com requests to mysite.com will not contain it. Be sure that both requests goes to the same domain and you will not have the Cookie issue.