How can I implement the functionality of 'Remember Me' on a login page? I want users who click on 'Remember Me' to be able to reopen the page without logging in again, even after closing their browser. But how do I differentiate between users who clicked 'Remember Me' and those who did not?
HTML
<form (ngSubmit)="onSubmit(form.value)" #form="ngForm">
<ng-content select='h3'></ng-content>
<label>
Email Address
<input type="email" name="email">
</label>
<label>
Password
<input type="password" name="password">
</label>
<label style="display: flex">
<input type="checkbox">
Remember me.
</label>
<button type="submit">Login</button>
</form>
Component
ngOnInit() {
this.AutoLogin();
}
onSubmit() {
this.auth.login(this.loginForm.value).subscribe((res) => {
localStorage.setItem('token', res['accessToken']);
localStorage.setItem('name', res['name']);
localStorage.setItem('role', res['role']);
localStorage.setItem('email', res['email']);
localStorage.setItem('userId', res['userId']);
this.toastrService.success('Successfully Login!');
this.router.navigate(['/home']);
},
(err) => {
});
}
AutoLogin(){
var accessTokenObj = localStorage.getItem("token");
console.log(accessTokenObj);
if (accessTokenObj) {
this.router.navigate(['/home']);
} else {
console.log("You need to login")
}
}