Home > Software engineering >  Output messages for the user
Output messages for the user

Time:02-02

I need your help. I use the toast library to display user messages for my authorization system. I have no mistakes, everything works, but incorrectly. The fact is that the message when I logged in comes, but when entering incorrect data does not work. Tell me, what am I doing wrong? Thank you very much

success_message: string = "You logged in"
error_message: string = "Incorrect data"

coustructor(private toastService: ToastrService) {}

show_success(success_message: string) {
   this.toastService.success(this.success_message)
}

show_error(error_message: string) {
   this.toastService.error(this.error_message)
}

loginToSystem() {
this.tokenSubscription = this.authSerivce.loginUser(
  this.loginForm.controls['username'].value,
  this.loginForm.controls['password'].value,
  this.rememberMeForm.controls['rememberMe'].value).subscribe(() => {
    if (this.tokenSubscription) {
      this.router.navigateByUrl(this.returnUrl)
      this.show_success(this.success_message)
      this.resetForm()
    } else if (!this.tokenSubscription){
      this.show_error(this.error_message)
    }
 })
}

CodePudding user response:

You should handle the error response in your error block of the subscription. So your code should look like this.

loginToSystem(): void {
        this.tokenSubscription = this.authSerivce
            .loginUser(
                this.loginForm.controls['username'].value,
                this.loginForm.controls['password'].value,
                this.rememberMeForm.controls['rememberMe'].value
            )
            .subscribe(
                (success: any) => {
                    this.router.navigateByUrl(this.returnUrl);
                    this.show_success(this.success_message);
                    this.resetForm();
                },
                (error: any) => {
                    this.show_error(this.error_message);
                }
            );
    }
  •  Tags:  
  • Related