I have an app that when the user logs in he received some data as a response, but if the response is not what I expect, the app crashes.
Let me show you the following:
I created an interface to tell Typescript the data I expect.
export interface AuthResponseData {
token: string;
expires: string;
role: number;
}
Then I created a method to send the login data to the server.
login(user: string, password: string){
return this.http.post<AuthResponseData>(
'https://localhost:44344/api/auth', { user: user, pwd: password }
).pipe(
catchError(this.handleError),
tap(resData => { this.handleAuthentication(resData.token, resData.expires, resData.role)})
);
}
I was expecting that if the response from the server didn't match the interface then the Angular would redirect to catchError
. But doesn't happen.
Now my question is: Is there any way to check if an API response is equal to the interface and throw an error if is not. Or what I'm asking isn't possible?
UPDATE:
After searching I discover that the interfaces disappear on run time, so there is no way to compare the api response with the interface (to the best of my knowledge). But I keep searching for a way to check the api response in a dynamic way. I think it is not really secure depending on api response to be always right. So I should check the API response. How can I do that?
Hope you can help me, thanks :)