scalafinaglefinatra

How to remove cookies on Finatra?


How do I remove a cookie after processing the request and building the response?

I have tried the following code, but it does not seem to work:

get("/login") { request =>
  val message = request.cookies.get("flash-message").map(_.value)
  request.removeCookie("flash-message")
  render.view(LoginView(message)).toFuture
}

I could not find any methods on ResponseBuilder that would remove a cookie, either.


Solution

  • It turns out, the way to do it, is the usual "JavaScript" way. Just create an expired cookie and send back, like this:

    import com.twitter.finagle.http.Cookie
    import com.twitter.util.Duration
    import java.util.concurrent.TimeUnit
    
    get("/login") { request =>
      val message = request.cookies.get("flash-message").map(_.value)
      val c = Cookie("flash-message", "")
      c.maxAge = Duration(-10, TimeUnit.DAYS)
      render.view(LoginView(message)).cookie(c).toFuture
    }
    

    Of course 10 days is just an arbitrary "duration" in the past.