I have an Ionic app that has a user provider with a signup() method:
doSignup() {
// set
I was using ngrx with Angular to store the token. I faced this error in my interceptor.ts.
The wrong code was this:
intercept(req: HttpRequest, next: HttpHandler): Observable> {
this._store
.pipe(
select(fromSelectors.selectAuth),
)
.subscribe((state: fromReducers.AuthState) => {
if (state.token != null) {
const clonedReq = req.clone({
headers: req.headers.set('Authorization', 'Bearer ' + state.token)
});
return next.handle(clonedReq);
}
else {
this.router.navigate(['login']);
}
});
}
I fixed the code by returning the flow:
intercept(req: HttpRequest, next: HttpHandler): Observable> {
return this._store
.pipe(
select(fromSelectors.selectAuth),
switchMap((state: fromReducers.AuthState) => {
if (state.token != null) {
const clonedReq = req.clone({
headers: req.headers.set('Authorization', 'Bearer ' + state.token)
});
return next.handle(clonedReq);
}
else {
this.router.navigate(['login']);
// return EMPTY stream
return of({}) as Observable>;
}
})
);
}