0

I have 2 pages: /home/success and /home/error. I want an user to be able to get to them only by redirecting from other page and I don't them them to be accessible directly.

# home controller  

def success = Action { r =>
    if (referer.isEmpty) Redirect(routes.Home.index) flashing "error" -> "From home#success: referer was null" // DEBUG
    else Ok(views.html.home.success(request.flash get "success" getOrElse ""))
  }

  def error = Action { implicit request =>
    if (referer.isEmpty) Redirect(routes.Home.index) flashing "error"  -> "From home#error: referer was null" // DEBUG
    else Ok(views.html.home.error(request.flash get "error" getOrElse ""))
  }

So far, so good - they are not accessible directly from browser. However, when I do this:

# other controller
def someAction = Action {
  // if everything is ok...
  if (success) Redirect(routes.Home.success) flashing "success" -> "success"

  //else if there is an error
 else Redirect(routes.Home.error) flashing "error" -> "error"
}

It always redirects me to /index with the flash "From home#success: referer was null" or "From home#error: referer was null" In other words, when I'm redirecting to other page, at that page referer is null.

Where referer is:

  def referer(implicit request: Request[AnyContent]): Option[String] = request.headers get "referer"

in my BaseController.

Even this doesn't work:

if (success) Redirect(routes.Home.success) flashing "success" -> "success" withHeaders "referer" -> "something"
else Redirect(routes.Home.error) flashing "error" -> "error" withHeaders "referer" -> "something"

What's up with that?

Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • I guess you can't capture the referer from redirect alone, eg. if you access your "other controller" directly, then the referer will be empty, but if you access "other controller" by clicking a link, then that origin page will be reflected in referer – andypp Feb 20 '14 at 05:38
  • @andypp, can't you see, I don't access my other controller directly? – Incerteza Feb 20 '14 at 05:40
  • "So far, so good - they are not accessible directly from browser. However, when I do this:" how do you access that "other controller"? is it by inputting the url directly or clicking link from another page? Another thing, setting referer with `withHeaders` wouldn't help since it only sets the response headers and not the request headers – andypp Feb 20 '14 at 05:58
  • The referer will depend on the page accessed before someAction, accessing someAction will not set referer in the request header, more info here http://stackoverflow.com/questions/2158283/will-a-302-redirect-maintain-the-referer-string/5441932#5441932 – andypp Feb 20 '14 at 06:29
  • @andypp - how do I deal with the situation when there is a form on a page which has action = "someAction" and I'm doing a redirect to "success" or "error" in "someAction"? This is my case. – Incerteza Feb 28 '14 at 04:13

0 Answers0