views:

507

answers:

4

My problem is, that the browsers' (IE&FF) autocomplete does not work for my login form.

I have a webapp with CakePHP & jQuery. To allow visitors to login/register unobtrusively. The login form is inside a div, which is loaded via AJAX. (This enables logging in without a page reload.)

The browsers do recognize it as a login field, as they prompt me to save the credentials when clicking login. And they really do save the username/password, as they appear between the saved ones in the browser settings. But the saved username/password is never entered automatically. They do not appear pre-entered when the page loads. When I start typing in the username, the username appears as a suggestion, but even when you select it, the password is not entered next to it. Why? How can I get this working?

That you can test it yourself, here is a simple AJAX login form:

http://gablog.eu/test/ajaxlogin.html

It loads the following login form, if you go to the url below, autocomplete will work for just the plain form, so it is not a problem with the form itself, but rather that it is AJAX loaded: http://gablog.eu/test/loginform.html

The layout:

<div id="user-bar">
    <script type="text/javascript">
        $(function() {
           $("#user-bar").load('loginform.html').html();
        });
    </script>
</div>

The view loaded (when not logged in):

<form id="form-login" action="" onsubmit="login(); return false;">
    <input type="text" id="username" name="username"/>
    <input type="password" id="password" name="password"/>
    <input type="submit" value="Login"/>
    <div id="login-error" class="error-message"></div>
</form>

<script type="text/javascript">
    function login() {
        $.post('/ajax/login', $("#form-login").serialize(), function(data) {
            if (data.success) {
                $("#user-bar").load('userbar.html').html();
            } else {
                $("#login-error").html(data.message);
            }
        }, "json");
    }
</script>

To clarify: I do not want to use AJAX autocomplete, I want the browser's autocomplete to work for my login form. This is an issue between my form and the browser. jQuery submission seems to play a minor role, as the usernames/passwords are saved. They are just not auto-entered for ajax loaded HTML elements! (The test site does not use jQuery submission.) Related question: http://stackoverflow.com/questions/1622628/browser-autocomplete-saved-form-not-work-in-ajax-request

+1  A: 

In case it helps, msdn says (towards the bottom of the page):

Note: if both of the following conditions are true:

  • The page was delivered over HTTPS
  • The page was delivered with headers or a META tag that prevents caching

...the Autocomplete feature is disabled, regardless of the existence or value of the Autocomplete attribute. This remark applies to IE5, IE6, IE7, and IE8.

I've emboldened the interesting bit.

.

karim79
sibidiba
@sibidiba - I would try `name="password"` and `name="username` for those input elements to see if that makes a difference.
karim79
@karim79 - Tried it, behavior remains the same.
sibidiba
@sibidiba - I'm all out of ideas. I'll leave my answer here for while before deleting, in case anyone else has the same idea. Sorry to have been of limited help :(
karim79
@karim79 Thanks anyway!
sibidiba
+1  A: 

I don't think you can get the form autocomplete to work if you load the form via ajax (security-wise I don't know if it can be really be abused or not, but the fact that a script could start loading fields into the page to see what data gets inserted doesn't look too good to me).

If you can, the best option would be to add a conditional block to the php file and include the form or not depending on whether the user is logged or not. If for some reason you can't do that, you might want to try to do a document.write() instead of the ajax call (and yes, using document.write is ugly :)

salgiza
Your arguments about security are well placed. But couldn't you steal information the same way without AJAX forms?Tying the form to a condition in PHP isn't a solution for me, because I need the form to be changed without page reload. Consider a site like stackoverflow, you start typing a question and realize, that you need to registered or log in. I want to allow visitors to register or log in without navigating away.
sibidiba
The thing with AJAX forms is that the javascript might come from a different source from the HTML (Cross-site scripting). If you logout from here you will find that stackoverflow does include the login form if you are not logged in. As most other websites that I've found, it just adds the user and password fields to the comment form, so that you can comment and login at the same time. Another solution that you could try would be to include the form with the page (hidden), and show it with jQuery, instead of loading it with an Ajax call.
salgiza
This could fall under xss and something like click hijacking, hence javascripts security restrictions might apply. What if another domains script could generate a look-a-like form and place it above the original form? The other domain would recieve the un/pw.
anddoutoi
What I do not understand, that if this is a security issue, then why not with normal, static forms as well? A script loaded from different domain could read the value of a password field of a static form that is autocompleted the same way (but of course could not load any form, I see).
sibidiba
@sibida. As I think you mention in your last comment, the problem is that you could make a mass creation of inputs in a page (imagine creating fields not only like "login" and "password", but things like "address", "phone number", and anything that the browser might autocomplete).I do agree, however, that it is pretty silly that it doesn't autocomplete the fields when you actually start typing in them, regardless of how they were created!
salgiza
+2  A: 

Autocomplete, in Firefox at least, triggers during page load. Adding the content afterwards would miss the window of opportunity.

A login form is tiny. I'd include it in the page from the outset and consider hiding it with CSS until it is wanted.

David Dorward
Thanks, now the login form is static and only its visibility is toggled. Works like a charm.
sibidiba
A: 

I see case when login form has to be pulled with ajax - if rest of the website loads as static html (cache). Login form (or authed user info) cant be displayed statically.

So your solution is to pull the form right after declaration (end tag) of the DOM element that serves as parent element for ajax-pulled loginform's html. Like (html tags replaced with []):

[div id="loginforms_parent"][/div][script language="javascript"]/ajax request and insert into DOM/[/script]

And browser has the form in DOM onLoad. Tested, firefox does autocomplete in that case.

abugabuga