I tried some ways but I couldn't solve it. If someone can give me an idea. I'm using environments to standardize the routes of my application.
My Interceptor:
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent} from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
import { TokenService } from '../token/token.service';
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
constructor(private tokenService: TokenService){}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(this.tokenService.hasToken()){
const token = this.tokenService.getToken();
req = req.clone({
setHeaders: {
'Authorization': 'bearer ' + token
}
});
}
return next.handle(req);
}
}
Module core:
import { CommonModule } from '@angular/common';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { ContainerFluidModule } from '../shared/components/container-fluid/container-fluid.module';
import { RequestInterceptor } from './auth/request-interceptor';
import { MenuComponent } from './menu/menu.component';
@NgModule({
declarations: [
MenuComponent,
],
exports: [
MenuComponent,
],
imports : [
CommonModule,
RouterModule,
ContainerFluidModule
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: RequestInterceptor,
multi: true
}
]
})
export class CoreModule {}
Edit post, to exemplify how the application is working, I will post how I managed to solve it and get the application ready in case I need to add a new API: D