I put this.useraccountsubject(user) to interpolate information on login, but I get an error.
ErrorType: this.useraccount.next(user) then Error An argument of type ‘HttpResponse’ is not
allowed against a balance of ‘Useraccount’
auth service.ts
export class AuthService {
private useraccountSubject: BehaviorSubject<Useraccount>;
public user: Observable<Useraccount>;
isLoggedIn = new BehaviorSubject(false);
constructor(private http:HttpClient, private router:Router){
this.useraccountSubject = new BehaviorSubject<Useraccount>(null as any);
this.user = this.useraccountSubject.asObservable();
if(sessionStorage.getItem("USER")){
const user =sessionStorage.getItem("USER");
if(user){
this.useraccountSubject.next(JSON.parse(user));
}
}
}
private handleError(err: HttpErrorResponse) {
if (err.status === 200) {
console.error('Error:',err.error.data)
} else {
console.error(`Backend error ${err.status}`)
}
return throwError(err);
}
private handleErrorsingup(err: HttpErrorResponse) {
if (err.status === 201) {
console.error('Error:',err.error.data)
} else {
alert('faild');
console.error(`Backend error ${err.status}`)
}
return throwError(err);
}
login(username:string,password:string){
const params = new FormData();
params.append('username', username);
params.append('password', password);
return this.http.post<any>(`${baseUrl}/signin/`, params, {observe:'response', withCredentials: true})//withCredentials:true
.pipe(map(user=>{
catchError(this.handleError)
//Here is the code section in question !!
this.useraccountSubject.next(user);
sessionStorage.setItem("USER", JSON.stringify(user));
this.isLoggedIn.next(true);
return user;
}))
}
signup(email:string,password:string,name:string ){
const params = new FormData();
params.append('email', email);
params.append('password', password);
params.append('name', name);
return this.http.post<any>(`${baseUrl}/signup/`, params, {observe:'response', withCredentials: true})
.pipe(
catchError(this.handleErrorsingup)
)
}
logout(){
return this.http.post<any>(`${baseUrl}/signout/`, {}).subscribe(
response=>{
this.isLoggedIn.next(false);
this.useraccountSubject.next(null as any)
sessionStorage.clear();
this.router.navigate(['login'])
}
)
}
public get useraccountValue(): Useraccount{
return this.useraccountSubject.value;
}
}
The format ‘HttpResponse’ does not have id, username, name, password, email attributes in the format ‘Useraccount’.
Useraccount.ts
export class Useraccount{
id:string ='';
username: string='';
name:string='';
password:string='';
email:string='';
}
I have for that format Useracount.ts