loginservice.ts
import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class LoginService {
constructor() { }
http = inject(HttpClient)
onLoginAuthentication(obj:any): Observable<Response>{
return this.http.post<Response>('https://fakestoreapi.com/auth/login', obj)
}
}
app.component.ts
import { Component, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { LoginService } from './login.service';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet, FormsModule],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent{
title = 'myProject';
username : string= ''
password : string= ''
service = inject(LoginService)
onSubmit(){
let obj = {
username: this.username,
password: this.password
}
this.service.onLoginAuthentication(obj).subscribe((response) => {
console.log(response);
sessionStorage.setItem('access_token', response.token);
});
}
}
Property 'token' does not exist on type 'Response'
The Response from the api is { token : "ajlsjdfflkjsdflk" } Instead of any type which alternative type should i use here