title: How to use session which is created by server in jQuery?
body: <p>First of all i have a website which is designed by me cause i am a front-end designer. I have API for the back-end server which was created by someone else. </p>
  
  <p>I have successfully made a request and got replied from server with auth token and it created session. Using jQuery Ajax, i made the call and i re-directed the user to some other page on successful HTTP status 201. But once redirected to some other page, it seems like my session got terminated.</p>
  
  <p>Now <strong>how can i use the session created by server and make the user to navigate through the site and once he clicks logout. The session should be over.?</strong></p>
  
  <p>In PHP we can use $_SESSION for these things and call this session variable in every page to check for the session. So if it's not there, we can redirect the user to login or show some message stating that session is over. </p>
  
  <p>But i want to do that using jQuery, i dont want to use PHP at all. How can i do that.</p>
  
  <p>Here's my form snippit.</p>
  
  <pre><code>&lt;form id="login-form" action="http://192.168.52.166:6000/api/2/users/login/" method="post"&gt;
  
  &lt;p&gt;&lt;label for="email"&gt;Email&lt;/label&gt;
  &lt;input type="email" name="email" id="email"&gt;&lt;/p&gt;
  
  &lt;p&gt;&lt;label for="password"&gt;Password&lt;/label&gt;
  &lt;input type="password" name="password" id="password"&gt;&lt;/p&gt;
  
  &lt;button value="Submit" type="submit"&gt;Login&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And here is my jQuery snippit</p>
  
  <pre><code>$('form').on('submit', function(event) {
  
      var data = {}
      $(this).serializeArray().map(function(x) {
          data[x.name] = x.value;
      });
  
      $.ajax({
          url: $(this).attr('action'),
          method: $(this).attr('method'),
          data: JSON.stringify(data),
          contentType: 'application/json',
          dataType: 'json',
          statusCode: {
              404: function() {
                  $("div.error-notif").show();
              },
              201: function() {
                  top.location.href = 'page.html';
              }
          },
          success: function(response) {
              console.log(response);
              jsonString = JSON.stringify(response);
              obj = JSON.parse(jsonString);
              $('div#json-response').append(obj.auth_token);
              $("div.success-notif").show();
              $("div.error-notif").hide();
              Cookies.set("authtoken", obj.auth_token);
              alert(Cookie("authtoken"));
          },
          complete: function(xhr, status) {
              console.log(status);
          }
      });
  
      return false;
  });
  </code></pre>
  
  <p>Note: i am using <a href="https://github.com/js-cookie/js-cookie" rel="nofollow noreferrer">JS Cookie</a> set the auth token which is recieved in response from server.</p>
  
  <p>and here is request and response which is taken from <a href="http://wireshark.org" rel="nofollow noreferrer">Wireshark</a>.</p>
  
  <pre><code>POST /api/2/users/login/ HTTP/1.1
  Host: 192.168.52.166:6000
  User-Agent: curl/7.47.0
  Accept: */*
  content-type: application/json
  Content-Length: 57
  
  {"email":"emjimadhu@email.com", "password":"qwerty123"}
  
  HTTP/1.1 201 CREATED
  Server: nginx/1.4.6 (Ubuntu)
  Date: Sun, 13 Nov 2016 22:00:22 GMT
  Content-Type: application/json
  Transfer-Encoding: chunked
  Connection: keep-alive
  Vary: Accept, Cookie
  X-Frame-Options: SAMEORIGIN
  Allow: POST, OPTIONS
  Set-Cookie: csrftoken=CSRF_TOKEN; expires=Sun, 12-Nov-2017 22:00:22 GMT; Max-Age=31449600; Path=/
  Set-Cookie: sessionid=6ob5doyoup7roob1o6o0burotayn4yt2; expires=Sun, 27-Nov-2016 22:00:22 GMT; httponly; Max-Age=1209600; Path=/
  
  21b
   {"auth_token":"my auth token", "id":"5f7g5a6r-6q9b-4544-91ce-f79e57d7e2ca","email":"emjimadhu@email.com"}
  </code></pre>
  
  <p>Now i have no idea what to do next. Let me re-pharse my question again.</p>
  
  <p>1) I want to call the API for login with values from forms. And server should listen with response in JSON if the user is authentic. (Which i done it already.)</p>
  
  <p>2) Now after the login, i need to redirect the user to other page. Which i done it too, with status code. </p>
  
  <p>3) In But once i redirect i have to make the session stay so the user can work on the website until he/she clicks logout.</p>
  
  <p>How can i do it?</p>
  
  <p>If my question is not understandable, please tell me. I will try to explain clearly.</p>