0

I have a sample angularjs login page, now I want to add functionality to my script for remembering password.

 <body ng-app="app" >
<div class="main">
    <div class="login-form">
        <h1>Login</h1>           
        <form ng-submit=submit()>
            <input type="text" name="username" placeholder="USERNAME" ng-model="person.firstName" required />
            <span class="error" ng-show="mainForm.usernamename.$error.required">required</span>
            <input type="password" name="pswd" placeholder="Password" ng-model="person.pswd" required />
            <span class="error" ng-show="mainForm.pswd.$error.required">required</span>
           <label>
                        <input name="remember" type="checkbox" data-ng-model="remember" data-ng-click="rememberMe()">   Remember Me
                    </label>
            <div class="submit">
                <input type="submit" value="LOGIN">
            </div>
            <p><a href="#">Forgot Password ?</a></p>
        </form>
    </div>   
</div>

<script>
  angular.module('app', [])

  var LoginCtrl= function ($scope) {

  $scope.submit = function () {          

      //return false;
  } };

Here is a demo too plunker:https://plnkr.co/edit/4WoGzXnzUfkBqzeFrTW9?p=preview

Will be thankful if anyone can provide code for this!!

Pranitha
  • 75
  • 1
  • 13
  • you could save the generated hash, which is a repesentation of the password in the localstorage or the cookies – Tobias Timm Apr 22 '16 at 05:48
  • Possible duplicate: http://stackoverflow.com/questions/18586938/remember-password-with-angularjs-and-ng-submit – Taku Apr 22 '16 at 05:52
  • @Tobias Timm: Iam not sure of how to do that.. Could you pls provide code for a better understanding – Pranitha Apr 22 '16 at 05:58
  • @user 013948: I require a checkbox and not autocomplete option. User should have a choice whether to make the password remembered or not. – Pranitha Apr 22 '16 at 06:01
  • you can refer the below link http://stackoverflow.com/questions/26645271/remember-me-functionality-and-token-in-angularjs – Nik Varma Apr 22 '16 at 06:07
  • you can refer this link http://stackoverflow.com/questions/26645271/remember-me-functionality-and-token-in-angularjs – Nik Varma Apr 22 '16 at 06:11
  • Please [edit] your post to include the code. While external links/demos might be helpful, they are no substitute for code in the post. – ryanyuyu Apr 22 '16 at 13:13
  • Code in your post is mandatory. The plunker link is optional but very helpful. Please copy the relevant code into your post from your plunker. – ryanyuyu Apr 22 '16 at 14:36

1 Answers1

0
I would use document.cookie with a factory code like this:

Creates a cookie (for example this one expires in a year):

app.factory('$remember', function() {
    return function(name, values) {
        var cookie = name + '=';
        cookie += values + ';';
    enter code here
        var date = new Date();date.setDate(date.getDate() + 365);cookie += 'expires=' + date.toString() + ';';
        document.cookie = cookie;
    }
});
This factory removes the cookie:

app.factory('$forget', function() {
    return function(name) {
        var cookie = name + '=;';
        cookie += 'expires=' + (new Date()).toString() + ';';
        document.cookie = cookie;
    }
});

$remember('my_cookie_name', response.user._id);
Nik Varma
  • 158
  • 9
  • Thanks a lot for the reply, but I want to know how to use the above in my code – Pranitha Apr 22 '16 at 06:42
  • check for submit event in your js file where you have written the code for the submit(inside your controller) and your login credential ok that time you have to set this parameter, if you want full code access then mail me i will work and send the whole code for you – Nik Varma Apr 22 '16 at 06:48
  • you should avoid to use $ for self-coded services/factories, because you can occur collisions with built-in factories and services that use the $ prefix. – Tobias Timm Apr 22 '16 at 10:01