0

I am trying to create a login form. Could anyone please suggest me how to add Remember me Functionality to the login page in Angular application?.....Your help is greatly appreciated!.. Thank you in advance! My login.ts file is as below,

ngOnInit() {
    this.form = this.formBuilder.group({
      email: ['', [Validators.required, Validators.email] ],
      password: ['', Validators.required]
    });
  }
  get f(){
    return this.form.controls;
  }
  onSubmit() {
    this.submitted = true;
   
    this.accountService.login(this.f.email.value, this.f.password.value)
}


and account.service.ts file is as below,

export class AccountService {
    private accountSubject: BehaviorSubject<Account>;
    public account: Observable<Account>;
    
    constructor(
        private router: Router,
        private http: HttpClient
    ) {
       this.accountSubject = new BehaviorSubject<Account>(null);
        this.account = this.accountSubject.asObservable();
    }
    public get accountValue(): Account {
        return this.accountSubject.value;
    }
    login(email: string, password: string) {
        return this.http.post<any>(`${baseUrl}/authenticate`, { email, password }, { withCredentials: true })
            .pipe(map(account => {
                this.accountSubject.next(account);
                return account;
            }));
    }
    logout() {
        this.http.post<any>(`${baseUrl}/revoke-token`, {}, { withCredentials: true }).subscribe();
        this.accountSubject.next(null);
        this.router.navigate(['/account/login']);
    }
   
}

Giannis
  • 1,790
  • 1
  • 11
  • 29

1 Answers1

0

Local/Session Storage or Cookies could do the trick. When 'Remember me' is set on login, save the session login credentials to either of the mentioned storages (probably encrypted). So, when the user revisits the site, your authentication services can check for the credentials and grant access to the user, otherwise, it logs out the user. You will probably have to extend the session token expiration from the server (or set it to infinity, or something like that). Therefore, you only clear the session creds when the user logs out themselves.

Memphis
  • 79
  • 1
  • 6
  • You should never store sensitive data in a cookie, such as user names, passwords, credit card numbers, and so on. Do not put anything in a cookie that should not be in the hands of a user or of someone who might somehow steal the cookie. https://stackoverflow.com/questions/2100356/is-it-secure-to-store-passwords-in-cookies – KLTR Jul 29 '21 at 12:58