I am using ngx-Admin with Angular 7 against a rest api developed in java with spring-boot. At the time of login, with my username and password, the rest api responds OK and gives me the token. Then, in my frontend, I created an HttpInterceptor to set my token on successive requests. So far there are no problems... the problem is that it doesn't send it. I set the token in the header and print it to the browser console, and it shows me correctly. In the next line, I send it and said header arrives without the token. Can you think of what could be happening? I leave you the code of my HttpInterceptor and some images of the console output.
import { Inject, Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
import { NbAuthJWTToken } from '@nebular/auth';
import { NbAuthService } from '@nebular/auth';
import { NB_AUTH_TOKEN_INTERCEPTOR_FILTER } from '@nebular/auth';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private injector: Injector,
@Inject(NB_AUTH_TOKEN_INTERCEPTOR_FILTER) protected filter) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// do not intercept request whose urls are filtered by the injected filter
if (!this.filter(req)) {
return this.authService.isAuthenticatedOrRefresh()
.pipe(
switchMap(authenticated => {
if (authenticated) {
return this.authService.getToken().pipe(
switchMap((token: NbAuthJWTToken) => {
const JWT = `Bearer ${token.getValue()}`;
req = req.clone({
setHeaders: {
Authorization: JWT
},
});
console.log('Ahora muestro el request!!!');
console.log(req);
return next.handle(req);
}),
)
} else {
// Request is sent to server without authentication so that the client code
// receives the 401/403 error and can act as desired ('session expired', redirect to login, aso)
return next.handle(req);
}
}),
)
} else {
return next.handle(req);
}
}
protected get authService(): NbAuthService {
return this.injector.get(NbAuthService);
}
}
In the following images I show you the results.
In the previous image you can see that I print the httprequest just before sending it.
In the previous image it is observed that the httprequest, just before sending it, has the desired values
And in the previous image we see that the request reaches the destination without the header and the token set. Can you think of what could be happening? Thank you all for at least reading it
I think I have found the solution to your problem, since the exact same thing happened to me. In this GitHub comment they explain how: https://github.com/akveo/ngx-admin/issues/1875#issuecomment-455308347
It seems that ng2-completer is interfering somehow and as of version 3.0.0-beta.2 they fixed it. I was having the same problem using ng2-smart-table 1.4.0 in ngx-admin (Angular 7) with ng2-completer 2.x and this fixed it. I am currently using version 3.0.2 of ng2-completer.
I hope it helps even if it's a little late.