Every time I submit my Login form, the return res; in the Login() method (found below) code gets executed twice.
The first response is a simple object { type: 0 }, the second is the actual response I'm looking for.
Why would return res; get called twice? How to I prevent that from happening?
I've stepped through the code and confirm that login() is only being called once, my HttpInterceptor is only being called once and the remote request is only being sent once.
I've upload this image to help explain what's happening. You can see that only 1 remote request has been executed, but 2 responses have been logged to the console.
This problem only started a few hours ago. I've been struggling with getting ionic serve to work with CORS, I tried using proxy.conf.json and proxy.js but without having access to the API I can't allow access. For development purposes I've disable CORS in Chrome using this extension because once I run the app on a mobile device it won't require CORS.
Additional toubleshooting I've tried:
- Disabled the Allow-Control-Allow-Origin: * extension in Chrome and the issue persists
- I removed the HttpInterceptor from the app.module.ts and the issue persists
- I've tried different remote URLs (even localhost) and the issue persists
- Shutdown Chrome and restarted PC and the issue persists
- Tried using FireFox and the issue persists
- I created a new Ionic4 Project and copy / pasted the same code and the issue persists
- I tried in Chrome on a seperate machine and the issue persists
Here is my login() observable that is subscribed to in my onSubmit() method:
login(username: string, password: string) {
const body = new FormData();
body.append('username', username);
body.append('password', password);
const request = new HttpRequest('POST', '[URL]', body);
return this.http.request(request)
.pipe(map(res => {
return res;
}));
}
Here is my onSubmit() and HttpInterceptor if it helps.
onSubmit () {
this.isSubmitted = true; // I've put a break point here to make sure this method is called once
if (!this.login.valid) {
return;
}
this.authenticationService.login(this.login.value.username, this.login.value.password).subscribe((res: any) => {
console.log(res); // this is the log that is displayed in the above screen shot
});
}
export class AuthInterceptor implements HttpInterceptor {
constructor() { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// Check auth here...
return next.handle(request);
}
}
I expect return res; to be called only once.