Angular 2 remember me functionality without using localstorage

jay patel picture jay patel · Nov 6, 2017 · Viewed 9.2k times · Source

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

Answer

AddWeb Solution Pvt Ltd picture AddWeb Solution Pvt Ltd · Nov 6, 2017

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