Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

page after login in playframework

In my play! app,I have coded the controllers.Security as

class Security extends controllers.Secure.Security {
...
   static void onDisconnected() {       
       Application.index();
   }
   static void onAuthenticated() {
      User user = User.find("byEmail",Security.connected()).first();
      if(user.isAdmin()) {
         Admin.index();
      }else {
          System.out.println("onAuthenticated()::user not admin");
   }
}

I have set the routes as

GET     /admin/?              Admin.index
*       /admin                module:crud
GET     /                    Application.index

When I am on a page say pageX and click on the login link,the login form appears and I am able to login.If I login as admin ,it takes me to the Admin.index() and thereby to Admin/index.html view.So far so good

But,when I am on pageX,and click on login link,I expect to come back to pageX.Instead ,the Application.index() method is called and I am taken to the Application.index.html.. Is this the expected behaviour? What do I have to do to get to pageX after login?

update:

I tried storing the url in flash using the @Before in Security controller

class Security extends controllers.Secure.Security {
   @Before
   static void storeCurrentUrl() {
      System.out.println("storeCurrentUrl()");
      flash.put("url", "GET".equals(request.method) ? request.url : "/");
   }
   static boolean authenticate(String username, String password) {
   ...
   }

   static void onAuthenticated() {
      ...
      String url = flash.get("url");
      System.out.println("url="+url);
      if(!user.isAdmin()) {
         if(url!=null) {
        System.out.println("url not null");
        redirect(url);
     }else {
       System.out.println("url null  ..go to /");
       redirect("/");
    }
      }
   }

When I login,I get these terminal output

url=null
url null  ..go to /
index()

I have put the login/logout links in main.html template which is inherited by all other pages

<div id="main">
    <div class="auth">
     <a href="@{Admin.index()}">Go to Admin Area</a><br/><br/>
     <a href="@{Secure.login()}">Login</a><br/><br/>
     <a href="@{Secure.logout()}">Log out</a>
</div>
like image 559
Damon Julian Avatar asked Sep 05 '11 04:09

Damon Julian


1 Answers

In you controller, before calling 'login()' put the 'url' into flash something like:

flash.put("url", "GET".equals(request.method) ? request.url : "/");

Once successfully logged in, get the 'url' and redirect.

String url = flash.get("url");
redirect(url); //you may redirect to "/" if url is null
like image 141
sojin Avatar answered Oct 22 '22 03:10

sojin