In Game! 2.0 with a sign of security, how do I redirect to the source URL after login?

With the game! framework 2.0 using the Security feature :

If I allow users to view several parts of the site that have not been authenticated, but they must authenticate with certain actions, how can I redirect them to the original URL before authentication, and not to the same URL for everyone?

This is a similar requirement for this question for Play! 1.x The Secure Playframework module is not redirected to the original URL after logging in .

However, the flash option for the source URL is not available in 2.0, as far as I can tell.

Basically the change I'm looking for will be in the authenticate method handler

def authenticate = Action { implicit request => loginForm.bindFromRequest.fold( formWithErrors => BadRequest(html.login(formWithErrors)), user => Redirect(routes.Application.index).withSession(Security.username -> user._1) ) } 

If you need some kind of Redirect (originalRequestUrl) .

Any ideas for a clean solution?

+6
source share
1 answer

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 ...

+11
source

Source: https://habr.com/ru/post/913562/


All Articles