Issue
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)
}
})
}
Solution
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);
}
);
}
Answered By - HassanMoin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.