2

I'm using typescript / angular2 in the frontend and nodejs in the backend.

I believe there is actually no problem with the backend itself. I believe the problem is in the frontend.

In an angular2 component I create the following client using '@angular/http'.

this.http
    .post(this.url, to_send)
    .map((res) => this.handleResponse(res) );

The request for the login is received on the server and I fix a variable to the session via

 req.session.cookie.something = "something";

However, when I make another call to the server, in the same or in a different component:

this.http
    .post(this.url, ANOTHER_CALL)
    .map((res) => this.handleResponse(res) );

The session does not exist in the server side (there is no 'something'. The session is not persistent).

My question is: does this.http from @angular/http automatically handle sessions / cookies in the frontend? What I'm I missing?

I really believe the problem is not in the backend because I've designed similar systems with nodejs before but if anyone thinks it might be because this works with them I'll try and post. However, and again, I really believe the problem must be here.

Fane
  • 1,978
  • 8
  • 30
  • 58
  • Please, someone help. I really need this and I have no idea on why this is happening... – Fane Dec 30 '16 at 01:26
  • I have no idea of what your Node code is doing, but you could at least investigate: open your browser dev tools, and go to the network panel. Does the response, supposed to have the cookie, really has the Set-Cookie header? Do the subsequent requests have the cookie header? Does the cookie contain what it should contain? – JB Nizet Dec 30 '16 at 08:00
  • @JBNizet I'm also facing similar issue in our project. I inspected in chrome network panel and see that the Set-cookie header is returned in response for my login request. After login I redirect to another page and from that page when any new request is made, the cookie header is not included among request headers. I would expect the cookie header to be added automatically by browser. Should I do something to make sure subsequent requests will pass a cookie header? – rineez Jul 21 '17 at 14:41
  • Is this the correct solution in this link? https://stackoverflow.com/questions/40286920/setting-request-cookies-angular2-http-post-request – rineez Jul 21 '17 at 14:47
  • @rineez check answer – Fane Jul 21 '17 at 17:07

1 Answers1

0

The solution is to use xhr.withCredentials = true in the request options

Fane
  • 1,978
  • 8
  • 30
  • 58
  • Here it is @rineez – Fane Jul 21 '17 at 17:07
  • I also faced another problem due to CORS after setting withcredentials=true and it was fixed by this solution: https://stackoverflow.com/questions/33483675/getting-express-server-to-accept-cors-request – rineez Jul 22 '17 at 06:14