I am currently working on creating a frontend GUI for an API. One of the key steps in this process involves polling an application for multifactor authentication. However, this particular aspect is not within my scope of work.
My objective is to develop an Angular 7 function that continuously polls or calls the HTTP GET /getmfa
API every 5 seconds until the multifactor authentication is successfully completed and the API returns a JSON response.
I have encountered some challenges in getting the interval functionality to work properly. There seems to be a lot of conflicting information on the internet regarding the syntax for RxJS versions and the use of .pipe
. I am using RxJS 6+ and Angular 7.
Initially, I attempted to implement the interval functionality with the following code:
poll(apiUrl: string, options?: any): Observable<any> {
let url = apiUrl;
let headers = new HttpHeaders({
'Content-Type': 'application/json',
});
let httpOptions = {
headers: headers,
withCredentials: true,
};
return interval(5000).pipe(map(() => {
return this.http.get(url, httpOptions)
.pipe(
map(
(data) => {
console.log('polling...');
console.log(data);
return data;
},
error => {
console.log('Error:', error);
}
)
);
}));
However, I found that even the timeout mechanism did not work as intended, but this approach was not aligned with my desired outcome anyway.
poll(apiUrl: string, options?: any): Observable<any> {
let url = apiUrl;
let headers = new HttpHeaders({
'Content-Type': 'application/json',
});
let httpOptions = {
headers: headers,
withCredentials: true,
};
setTimeout(() => {
return this.http.get(url, httpOptions)
.pipe(
(data) => {
return data;
}
);
}, 5000);
Could someone provide insights on where I might be making mistakes in my implementations?