Is there any way to make remember me functionality without use of local storage in angular 2?
Asked
Active
Viewed 1.1k times
3
-
2what about a cookie? – Yoann Augen Nov 06 '17 at 09:57
-
1https://stackoverflow.com/questions/244882/what-is-the-best-way-to-implement-remember-me-for-a-website?rq=1 – Muhammed Misir Nov 06 '17 at 09:59
-
Possible duplicate of [What is the best way to implement "remember me" for a website?](https://stackoverflow.com/questions/244882/what-is-the-best-way-to-implement-remember-me-for-a-website) – Igor Nov 06 '17 at 10:42
-
localstorage is best way – AddWeb Solution Pvt Ltd Nov 06 '17 at 13:21
-
i am using localstorage for login and logout purpose – Jay Patel Nov 06 '17 at 13:23
1 Answers
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
-
1But 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