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?