james
james

Reputation: 1753

play framework how to do separate routing for same URL ?

I have built a site that when a user fills in a form correctly he is redirected to a confirmation page that has the url mySite.com\A\confirmation.

I want to be able to redirect the user to a different page If he enters that url "mySite.com\A\confirmation" himself

What is the best way of achieving this (i was hoping something more elegant then keeping state of each user)

Upvotes: 5

Views: 823

Answers (3)

Pere Villega
Pere Villega

Reputation: 16439

It won't be simple, as you may find lots of edge cases.

When you do a redirect, Play sends a 303 HTTP code and the browser does a new GET request against the URL. You could try to add a parameter to check, but then the user could add that to the navigation bar and the request will work.

Also, you would be violating Idempotence in GET. Not a good thing to do, as browsers may rely on it.

Better add a check in the same page and if the form has not been submitted, just show a message or throw a 404 error (not found).

Upvotes: 2

Mike
Mike

Reputation: 2434

You could put a token in the flash scope and redirect to mySite.com\A\confirmation

If the token is not there redirect him to another page, otherwise display the page and keep the token in the flash scope. If you keep the token in the flash scope, the user will be able to reload the page without being redirected.

Upvotes: 2

Alexander Ponomarenko
Alexander Ponomarenko

Reputation: 559

Possible simple solutions:

  1. Store some key in cookie, verify it on confirmation page and clear. Cookie value should be encrypted, so user won't be able to forge it (if not, you can use Crypto.encryptAES(String), Crypto.decryptAES(String) manually.
  2. Change confirmation controller to accept parameter, pass that parameter from form submit controller: confirmation(secureParameter); Validate parameter in confirmation method.
  3. Do POST request to confirmation from your code and change routes and redirect on GET requests
  4. Instead of redirect to confirmation in your controller, simply render confirmation template directly, this way there would be no confirmation URL.

Upvotes: 4

Related Questions