3

Is there any way to make remember me functionality without use of local storage in angular 2?

Jay Patel
  • 305
  • 2
  • 6
  • 20

1 Answers1

5

You can use angular2-cookie remember me functionality. please install below plugin

npm install angular2-cookie

set cookie in your project using below link

https://www.npmjs.com/package/angular2-cookie

Component File:

public Formdata:any = {};

export class AppComponent {
  constructor(private _cookieService:CookieService) {

    if(_cookieService.get('remember')) {
       this.Formdata.username=this._cookieService.get('username');
       this.Formdata.password=this._cookieService.get('password');
       this.Formdat.rememberme=this._cookieService.get('remember');
    }

  }

  submitData() {
     this._cookieService.put('username',this.Formdata.username);
     this._cookieService.put('password',this.Formdata.password);
     this._cookieService.put('remember',this.Formdat.rememberme);
  }
}

View File :

<form>
  <div>
     <label>Username : </label>
     <input type="text" [(ngModel)]="Formdata.username" />
  </div>
  <div>
     <label>Password : </label>
     <input type="text" [(ngModel)]="Formdata.password" />
  </div>
  <div>
     <input type="checkbox" [(ngModel)]="Formdata.rememberme" /> Remember me 
  </div> 
<button (click)="submitData()">Submit</button>
</form>

Thanks

Sushmit Sagar
  • 1,412
  • 2
  • 13
  • 27
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • Property 'set' does not exist on type 'CookieService'. i am getting this in cookieservice – Jay Patel Nov 06 '17 at 13:24
  • In Angular6 is not working with `.put`. It worked with `.set`. Thank you! – Mr. Wizard Aug 01 '18 at 13:39
  • 1
    But this is not working very good. if I login with `account1` and then with `account2`, it won't remember passwords from both of them.. just from the last one I logged in, which is `account2`. Is this what you wanted to achieve or is it there another solution? – Mr. Wizard Aug 01 '18 at 14:13