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.

5
  • Please, someone help. I really need this and I have no idea on why this is happening... Commented Dec 30, 2016 at 1: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? Commented Dec 30, 2016 at 8: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? Commented Jul 21, 2017 at 14:41
  • Is this the correct solution in this link? stackoverflow.com/questions/40286920/… Commented Jul 21, 2017 at 14:47
  • @rineez check answer Commented Jul 21, 2017 at 17:07

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

Here it is @rineez
I also faced another problem due to CORS after setting withcredentials=true and it was fixed by this solution: stackoverflow.com/questions/33483675/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.