0

My app is scraping data from a third party site wherein it needs to be logged in first before it could scrape the data. The thing is, there is a session expiration so the app needs to check if the session is already expired. If the session is not yet expired, $http's JSESSIONID cookie will be the same as the one returned after logging in. If it is expired, the value of JSESSIONID changes.

What I'm planning to do is that after logging in, I'll store the JSESSIONID to the localStorage. After every request of $http, I'll compare the new JSESSIONID to the stored JSESSIONID. I'll relogin the user if it's not the same(means session is expired).

However, I couldn't get the cookie. It returns undefined. I'm also thinking, is it because the cookie is on the request header instead of the response header? If yes, how could I get it?

enter image description here enter image description here

   $http({
                method: 'POST',
                url: url,
                data: params
            }).then(function (response) {
                console.log($cookies.get('JSESSIONID'));
                callback.success(Parser.getGrades(response.data));
            }, function (error) {
                callback.error();
            });

I already included ngCookies, no errors but the value is undefined. I tried to use getAll() and it returns:

Object {_ga: "GA1.1.1725476839.1445841661"}

Configurations of my $httpProvider:

$httpProvider.defaults.withCredentials = true;
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
dgzz
  • 2,929
  • 6
  • 34
  • 56
  • With withCredentials = true, cookie management will be done automatically. You should not need to use ngCookies unless absolutely necessary. – ArslanW Oct 28 '15 at 17:21
  • I'm scraping data from another site. I'm trying to check if my session is still valid. – dgzz Oct 28 '15 at 18:04
  • This link might help http://stackoverflow.com/questions/12624181/angularjs-how-to-set-expiration-date-for-cookie-in-angularjs – ArslanW Oct 28 '15 at 18:15
  • I updated my question – dgzz Oct 28 '15 at 19:11
  • It might be HttpOnly cookie, they are not accessible from JS https://en.wikipedia.org/wiki/HTTP_cookie#HttpOnly_cookie – Can Guney Aksakalli Oct 28 '15 at 19:14
  • Is this a limitation of JS? I have a native version of this app (Android - Java), I could easily access the cookie by having a persistent cookiestore – dgzz Oct 29 '15 at 04:18
  • @DanielJohnGomez It could be the case for the browser but I am not sure about Ionic Platform (Cordova). This post might be helpful: http://blog.ionic.io/angularjs-authentication/ – Can Guney Aksakalli Oct 31 '15 at 01:03

0 Answers0