0

Is it possible to set cookies in plumber when dealing with cors connections?

I have an angular app (say running on localhost:4200) and plumber api (say on localhost:8000), just trying to use the example code from documentation:

#* @get /sessionCounter
function(req){
  count <- 0
  if (!is.null(req$session$counter)){
    count <- as.numeric(req$session$counter)
  }
  req$session$counter <- count + 1
  return(paste0("This is visit #", count))
}

I register the sessionCookie() hooks on the rooter, so this works when I visit localhost:8000/sessionCounter in browser, but not from the angular app.

Is there any setting I am missing here?

I only added res$setHeader("Access-Control-Allow-Origin", "http://localhost:4200") for serving cors requests, do I need something else?

mohan08p
  • 5,002
  • 1
  • 28
  • 36
h1427096
  • 273
  • 1
  • 9

1 Answers1

0

I'm not sure off the top of my head what the constraints are for setting a cookie across CORS boundaries, or whether or not it's allowed.

One workaround, though, would be to host both services on the same port. If your Angular app is just static, you could just have Plumber serve the HTML/JS assets itself, so that your whole app is all running on a single port. Here's one example: https://github.com/trestletech/plumber/tree/master/inst/examples/06-sessions . Here's another using a Vue.js app hosted from Plumber: https://github.com/trestletech/movies-plumber

If that's not possible, you could also consider proxying traffic from your "main" web server where you host your angular app through to Plumber -- again on the same port/domain.

Jeff Allen
  • 17,277
  • 8
  • 49
  • 70
  • Thanks Allen, I'll probably just send a token in the response for now and then include it in every request. Hope I'll figure it out how to include http header :) I'll play with this a little later, I'm just checking what is possible right now. – h1427096 Jan 19 '18 at 09:54