You can use the Referer header or explicitly wrap the return URL into an authentication action:
Using the Referer Header
def authenticate = Action { implicit request => loginForm.bindFromRequest.fold( errors => BadRequest(html.login(errors)), user => { val returnUrl = request.headers.get(REFERER).getOrElse(routes.Application.index.url) Redirect(returnUrl).withSession(Security.username -> user._1) } }
Explicit return URL passing
def authenticate(returnUrl: String) = Action { implicit request => loginForm.bindFromRequest.fold( errors => BadRequest(html.login(errors, returnUrl)), user => Redirect(returnUrl).withSession(Security.username -> user._1) ) }
Using the HTTP Referer header, you should still handle the case where the browser does not fill this header, and by explicitly transferring the return URL as a parameter to your authenticate action, you may have more template in the processing code using authentication ...
source share