Question

How would I save the "secret token" as a cookie before redirecting so that I can retrieve it later?


@Mustafa Simav's answer is correct. Everything strangely started working when I cleaned my project (deleted sbt generated files)

Was it helpful?

Solution

First of all, I don't now details of OAuth so I am not sure whether it is good idea to store token on cookie or not. However if you want to store and retrieve cookie with spray, you can do it like this:

val routes =
  path("redirect") {
    setCookies(HttpCookie("random_token", random_token)) {
      redirect("twitter.com/authorize", Found)
    }
  } ~
  path("get-cookie") {
    parameters('token) { token =>
      optionalCookie("random_token") {
        case Some(random_token) if random_token == token => complete("OK")
        case _ => complete("Error", BadRequest)
      }
    }
  }

PS: Don't forget to concatenate routes with ~

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top