Wednesday, June 1, 2016

Re: Getting username from url only works after refresh

For some reason, passing the username around via the URL seems like a code smell to me.

I'm not sure if it's actually better or not, but I store the User object from the server (less password hash) in a singleton (and the session, in case of a full refresh).  I also have all my Place content setup so I just change it within a Panel (TabLayoutPanel, though Dock or Stack would work, too) while leaving navigation and header alone.

This removes putting user information in the URL, so people can share deep-links to content without sharing usernames by default.

On Wednesday, June 1, 2016 at 7:09:20 AM UTC-5, Olar Andrei wrote:
Hello,

I've tried a little trick today, because I still belive the browser downloads code he does not need before he actually needs it.

I placed this:
Window.Location.reload();
inside my UserViewImpl, so it would reload after the login.

The problem is that when the application starts with the LoginViewImpl the page refreshes every second. So I'm not even in the UserViewImpl, so I didn't log in, and the page gets the code from the UserViewImpl and starts refreshing.
The thing is I've used code splitting in my application like this:

Login Button

loginButton.setHTML(LoginFormConstants.LOGIN_BUTTON);
loginButton.addClickHandler(new ClickHandler() {

@Override
public void onClick(ClickEvent event) {
final String username = usernameBox.getText();
final String password = passwordBox.getText();

                                // Is this good code splitting ?
GWT.runAsync(new RunAsyncCallback() {

@Override
public void onSuccess() {
performUserConnection(username, password);
}

@Override
public void onFailure(Throwable reason) {
// TODO Auto-generated method stub
}
});
}
});

 and performUserConnection()

private static void performUserConnection(String username, String password) {
DBConnectionAsync rpcService = (DBConnectionAsync) GWT.create(DBConnection.class);
ServiceDefTarget target = (ServiceDefTarget) rpcService;
String moduleRelativeURL = GWT.getModuleBaseURL() + "DBConnectionImpl";
target.setServiceEntryPoint(moduleRelativeURL);

rpcService.authenticateUser(username, password, new AsyncCallback<User>() {

@Override
public void onSuccess(User user) {

if (user.getType().equals("User")) {
String username = user.getUsername();
presenter.goTo(new UserPlace(username));
} else if (user.getType().equals("Admin")) {
String username = user.getUsername();
presenter.goTo(new AdminPlace(username));
}
}

@Override
public void onFailure(Throwable caught) {
DialogBox dialogBox = createDialogBox();
dialogBox.setGlassEnabled(true);
dialogBox.setAnimationEnabled(true);
dialogBox.center();
dialogBox.show();
}
              });
      }

So the page should wait to see if connection was successful, and then download the code, right ? Why isn't this happening ?

Thanks in advance


marți, 31 mai 2016, 01:25:10 UTC+3, Olar Andrei a scris:

My login based application, requires to always know the username of the logged in user. (MVP) . So I'm getting the username from the url, but when the page opens after the login succeeded, I can't get the username from the url, because it does not appear to exists, but it is there. It only works after a refresh. Then I'm able to get the username.


The URL is in the form http://127.0.0.1:8888/AdministrareBloc.html#AdminPlace:admin, where I'm splitting the String to only get the admin part.


I thought this is because it downloads the code before verifying the user. So I placed a split point in my code like this: (I don't know if I placed it correctly)


loginButton.addClickHandler(new ClickHandler() {            @Override          public void onClick(ClickEvent event) {              final String username = usernameBox.getText();              final String password = passwordBox.getText();              GWT.runAsync(new RunAsyncCallback() {                    @Override                  public void onSuccess() {                      performUserConnection(username, password);                  }                    @Override       
Introduceți aici codul...
public void onFailure(Throwable reason) {
                    // TODO Auto-generated method stub                  }              });          }      });      private static void performUserConnection(String username, String password) {      DBConnectionAsync rpcService = (DBConnectionAsync) GWT.create(DBConnection.class);      ServiceDefTarget target = (ServiceDefTarget) rpcService;      String moduleRelativeURL = GWT.getModuleBaseURL() + "DBConnectionImpl";      target.setServiceEntryPoint(moduleRelativeURL);        rpcService.authenticateUser(username, password, new AsyncCallback<User>() {            @Override          public void onSuccess(User user) {                if (user.getType().equals("User")) {                  String username = user.getUsername();                  presenter.goTo(new UserPlace(username));              } else if (user.getType().equals("Admin")) {                  String username = user.getUsername();                  presenter.goTo(new AdminPlace(username));              }          }        }  }


This is happening when the user clicks the login button. Is the split point placed correclty, or not ? How can I get the username without needing to refresh the page after a successful login ?

Thanks in advance

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment