Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let browser save username/password values in a login <form>?

Tags:

gwt

I have a GWT app, and need a user login form. I want to let the browser save the username and password for the user. I believe I need to use a 'regular' form for this (not one generated by GWT). So I made a simple form:

<form id="myform">
  <input type="text" name="username">
  <input type="password" name="password">
  <input type="submit" value="Login" />
</form>

Now I'd like to interrupt the submission process, grab the username/password, do login via RPC, but have the browser save the contents of those fields for the user, if they want. There are a few posts on the GWT dev boards about it, not sure which one works because none of them are working out of the box for me. I think it should look like:

FormPanel form = FormPanel.wrap(Document.get().getElementById("myform"), false);
form.setAction("javascript:;");
form.addSubmitHandler(new SubmitHandler() {
    public void onSubmit(SubmitEvent event) {
        // Do my RPC call here?
        // User should have been prompted to save password already now?
    }
});

Does anyone know how to get this to work?

like image 284
user246114 Avatar asked May 06 '10 03:05

user246114


1 Answers

This depends on how your form (myform) looks because every browser uses its own heuristic to discover a potential login form with credentials to store (Chrome for example needs an action attribute present in the markup http://code.google.com/p/chromium/issues/detail?id=29513). So what we did is to google for remember credentials feature for every browser our app must support. The result was the following form element:

<form id="login-form" style="display:none" action="login">
    <input type="text" tabindex="0" name="username" id="login-username">
    <input type="password" tabindex="0" name="password" id="login-password">
    <input style="position: absolute; left: -9999px; width: 1px; height: 1px;" type="submit">
</form>

May work for you as well.

EDIT

Here an example that works for me (only tried FF version 14.x). Don't forget to remove the display:none from the form tag.

public class Starter implements EntryPoint {

    private static final String FORM_ID = "login-form";
    private static final String USER_ID = "login-username";
    private static final String PASSWORD_ID = "login-password";

    @Override
    public void onModuleLoad() {
        injectLoginFunction();
        TextBox fUsername = TextBox.wrap(DOM.getElementById(USER_ID));
        fUsername.setStyleName("gwt-TextBox");
        PasswordTextBox fPassword = PasswordTextBox.wrap(DOM.getElementById(PASSWORD_ID));
        fPassword.setStyleName("gwt-PasswordTextBox");
        FormPanel fLoginForm = FormPanel.wrap(DOM.getElementById(FORM_ID), false);
        fLoginForm.setAction("");
        fLoginForm.addSubmitHandler(new SubmitHandler() {

            @Override
            public void onSubmit(SubmitEvent event) {
                Window.alert("test");
            }
        });
        fLoginForm.getElement().setAttribute("onsubmit", "login();return false;");
    }

    private static void doLogin() {
        Window.alert("test");
    }

    private static native void injectLoginFunction() /*-{
        $wnd.login = function() {
            @test.client.Starter::doLogin()();
        };
    }-*/;
}
like image 194
z00bs Avatar answered Sep 18 '22 12:09

z00bs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!