Remember Password with AngularJS and ng-submit

17,928

Solution 1

The problem is the dynamically generated login form. After putting the form into the index.html it worked as expected. I guess this is a security issue.

The problem that then occurred was that the ngModels didn't get updated on autofill. After some searching I found the solution to that problem here. In AngularJS 1.2+ this is supposed to be fixed.

Solution 2

Your code is ok, but you need to add the name attributes to your inputfields, such as:

<input type="text" name="username" ...>

and

<input type="password" name="password" ...>

Solution 3

Your form HTML is a bit confusing.

<form action="/#/dashboard/login" onsubmit="return false;" ng-submit="login()" name="loginForm">

When the form is submitted do you want it to go to /#/dashboard/login or do ng-submit="login()" ? At the moment, the ng-submit is being ignored in favour of the form action. If you want it to go to /#/dashboard/login as a new page, then just remove the ng-submit and onsubmit attributes and it will work as normal.

If you want it to do ng-submit="login()", then remove the action and onsubmit attributes. Angular automatically prevents form submission when a form with ng-submit does not have an action attribute too. Doing it this way will stop the browser remember password prompt as the form isn't actually submitted anywhere. I guess this is an area where browsers have yet to catch up to the era of the single page application, there's no direct fix for it that I'm aware of.

A workaround would be to have a separate hidden form in the HTML, set the username/password there to the same as the user enters in main form, and then submit that hidden form to an iframe at the same time as ng-submit is called - have a look at How can I get browser to prompt to save password? for ideas about how to do it.

Solution 4

I didn't have to do anything special. But I noticed that while MS Edge and Firefox worked well and offered to remember credentials Chrome didn't.

So simply by providing name attribute to the login form and to username and password it seemed to work fine in Chrome. Autocomplete is on as well. Example:

<form method="post" class="form-horizontal well" ng-submit="login()">
        <div class="form-group">
            <label class="col-sm-4 control-label">Email Address</label>
            <div class="col-sm-8">
                <input name="username" ng-model="email" type="email" class="form-control" placeholder="[email protected]" autofocus="autofocus" autocomplete="on" required />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">Password</label>
            <div class="col-sm-8">
                <input name="password" ng-model="password" type="password" autocomplete="on" class="form-control" required />
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-4 col-sm-8">
                <button type="submit" class="btn btn-primary">Log on</button>
            </div>
        </div>
    </form>

PS: I'm using Chrome Version 45.0.2454.93 m

Share:
17,928

Related videos on Youtube

soelu
Author by

soelu

Updated on June 06, 2022

Comments

  • soelu
    soelu almost 2 years

    How do I get the browser to ask the user to remember the password when using ng-submit in an AngularJS single page application.

    My Form:

    <form action="/#/dashboard/login" onsubmit="return false;" ng-submit="login()" name="loginForm">
        <input type="text" required id="username" name="username" ng-model="username" autocomplete="on" placeholder="Username" value="">
        <input type="password" required id="password" name="password" ng-model="password" autocomplete="on" placeholder="Password" value="">
        <button type="submit" class="btn">Submit</button>
    </form>
    

    Any Ideas?


    UPDATE

    I just added the action to get the browser to recognise the form and trick it into remembering the password. (which obviously didn't work.) The form works fine without the action. The onsubmit="return false;" prevents the execution of the action. Only the ng-submit is doing anything.

    • smhg
      smhg over 10 years
      This is an issue with dynamically generated forms (in any framework).
    • Márton Tamás
      Márton Tamás over 7 years
      Note that some browsers in private mode won't show de remember password dialog (in my case the problem was that I tested with Firefox private browsing).
  • soelu
    soelu over 10 years
    I just added the action to get the browser to recognise the form and trick it into remembering the password. the form works without the action. the onsubmit="return false;" prevents the execution of the action. only the ng-submit is doing anything.
  • soelu
    soelu over 10 years
    the form works without the action and without the onsubmit handler. but still no remember password prompt.
  • smhg
    smhg over 10 years
    The name attributes, if you look at code in the question, are present. Without name attributes, there wouldn't be any completion in regular (non-JS-generated) forms either.
  • Fernando Vieira
    Fernando Vieira almost 10 years
    When you have an action" attribute, the ng-submit doesn´t work! Quote from AngularJS API Ref: "For this reason, Angular prevents the default action (form submission to the server) unless the <form> element has an action attribute specified."