I create a function that is responsible for logging in. it looks like: (authService code)
login(username: string, password: string) {
return new Promise((resolve, reject) => {
this.checkUser(username, password).subscribe((output) => {
if (output.token) {
console.log('Log in true');
resolve();
} else {
reject();
}
});
});
}
private checkUser(username: string, password: string): Observable<any> {
return this.http.post<any>('api/auth', {username, password});
}
I wanted to use Promise with resolve and reject to handle success and failure event in the call 'login' function, but I don't know why never in "if" dosn't go to the "else" section. AND I do not know why onSubmitSucces and onSubmitFailure execute in same time...
(loginComponent code)
onSubmit() {
this.authService.login(this.username, this.password)
.then(this.onSubmitSuccess(), this.onSubmitFailure());
}
private onSubmitSuccess(): any {
console.log('Success');
}
private onSubmitFailure(): any {
console.log('Failure');
}