I have been working on implementing a login feature with angular5, but I have encountered several issues along the way. While I managed to resolve some of them, I am currently facing a compilation problem:
ERROR in src/app/login/login.component.ts(38,3): error TS2322: Type 'Subscription' is not assignable to type 'Boolean'.
Types of property 'valueOf' are incompatible.
Type '() => Object' is not assignable to type '() => boolean'.
Type 'Object' is not assignable to type 'boolean'.
I understand the error message, however, I am struggling to figure out how to fix it and get my code to function properly. Below is the code snippet from the login.component.ts class:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NgForm } from '@angular/forms';
import { FormGroup, FormControl } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpErrorResponse } from '@angular/common/http';
import { UserService } from '../auth/user.services';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
ngForm: NgForm;
login: string;
password: string;
myForm: FormGroup;
isLoginError : boolean = false;
connected : Boolean = false;
constructor(private userService: UserService, private router: Router) { }
ngOnInit() {
}
// Execute this when submit login form
OnSubmit(form){
console.log(form.value);
console.log(form.value.login);
this.connected = this.userService.authenticate(form.value.login,form.value.password).subscribe((data : any)=>{
return data.password = form.value.password;
//return true;
});
} }
The error occurs at line 38 which corresponds to
this.connected = this.userService.authenticate(form.value.login,form.value.password).subscribe((data : any)=>{
In user.service.ts, the authenticate method is defined as follows:
authenticate(login:string, password:string) :Observable<Boolean> {
console.log('Authenticate ....');
const credentials = {login:login, password:password};
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
console.log('Appended content type ....');
console.log(' Login '+login+' Password '+password+' Headers '+headers);
return this.http.get<any>(
'/users?login?'+JSON.stringify(login),
{ headers }
)
.map(user => user.password === password);
}
Can someone help me resolve this error and make my code work correctly?