Cookie not set on cross-domain

I am making angularjs cordona apps. To log in, we have to make the first request on a different domain /login

, and the response has a name entry cookie

that contains a cookie that we have to set to log in.

The problem is that when I develop with ionic serve

(so in my browser) it's a terrible thing called CORS. And since the other site I want to connect to doesn't allow CORS, I was forced to use a proxy using node-http-proxy

that proxied requests and added CORS headers. Then when I needed to connect to another website I made requests to my proxy. But now I need to login as explained earlier.

So I put this code in my proxy, which parses the route response /login

and just fetches the cookie:

connect.createServer(
  function(req, res, next) {
    res.oldWrite = res.write;
    var body = ""
    res.write = function(data) {
      body = data.toString('UTF8')
      cookie = (/<cookie><!\[CDATA\[wenvjgol=(.+)\]\]><\/cookie>/g.exec(
        body) || ["", undefined])[1]
      if (cookie) console.log('found cookie: ' + cookie)
      res.oldWrite(data);
    }
    ...
    if (cookie) {
      res.setHeader('Set-Cookie', 'wenvjgol=' + cookie +
        '; Domain=otherwebsite.com; Path=/; Expires=' + new Date(Date.now() +
          1000 * 3600 * 24).toUTCString())
    }
    next();
  },
  function(req, res) {
    if (req.method === "OPTIONS") {
      res.end();
      return;
    }
    proxy.web(req, res);
  }
).listen(8101);

      

Now that I reach /login

I can see cookie found: 1gmriba47wlc4ow4k88k8o8044gskso8sc4s0c8k40gcs44g8og40c8sks00so8og

The cookie was not set on the first request for some reason. So I make the first request somewhere on another server (always through a proxy).

The response headers have

Set-Cookie: wenvjgol=1gmriba47wlc4ow4k88k8o8044gskso8sc4s0c8k40gcs44g8og40c8sks00so8og; Domain=anotherdomain.com; Path=/; Expires=Tue, 21 Oct 2014 11:23:55 GMT

      

And chrome detects it. But when I want to make a request to a url and you need to login, neither cookie, nor in the request nor in the response :(

This is how I do it in angular:

$auth.login($event).then (sid) ->
  console.log 'sid is', sid

  $http(
    method: "GET"
    url: "#{config.domain}/forums"
    withCredentials: true
    # headers:
      # "Cookie": "wenvjgol=#{sid}"
  )
.then ->
  $http(
    method: "GET"
    url: "#{config.domain}/forums/5-#{$routeParams.id}-#{$routeParams.topic}-1-0-1-0-0.xml"
    withCredentials: true
    # headers:
      # "Cookie": "wenvjgol=#{sid}"
  )
.then console.log

      

The complete proxy file is here: https://gist.github.com/vinz243/fd01b77ef309101976a5

I hope I get it.

+3


source to share





All Articles