diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 1dd9a2ed7f707462ea9e666b6c939330cc13522c..f39474125b4b81dd73210feb34104b9d9d4906fa 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,9 +1,12 @@ import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; +import { HTTP_INTERCEPTORS } from '@angular/common/http'; + import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { HeaderComponent } from './shared/components/header/header.component'; +import { HttpInterceptorService } from './shared/interceptors/http-interceptor.service'; @NgModule({ declarations: [ @@ -14,7 +17,7 @@ import { HeaderComponent } from './shared/components/header/header.component'; BrowserModule, AppRoutingModule ], - providers: [], + providers: [{ provide: HTTP_INTERCEPTORS, useClass: HttpInterceptorService, multi: true },], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/config/api-config.ts b/src/app/config/api-config.ts new file mode 100644 index 0000000000000000000000000000000000000000..8c6c5d2067a325c46114a71ee559bc947b1cf10a --- /dev/null +++ b/src/app/config/api-config.ts @@ -0,0 +1,13 @@ +import { environment } from "src/environment/environment"; + +export const apiConfig = { + + apiBaseUrl: environment.baseURL, + // eslint-disable-next-line max-len + token: '', + catalog:{ + getCatalog:{ + 'api-url': environment.backendURL + 'catalog', + } + } +} \ No newline at end of file diff --git a/src/app/shared/interceptors/http-interceptor.service.spec.ts b/src/app/shared/interceptors/http-interceptor.service.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..e20bbefc07eddd23ab025f85cbe183c81d8f6b7d --- /dev/null +++ b/src/app/shared/interceptors/http-interceptor.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { HttpInterceptorService } from './http-interceptor.service'; + +describe('HttpInterceptorService', () => { + let service: HttpInterceptorService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(HttpInterceptorService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/shared/interceptors/http-interceptor.service.ts b/src/app/shared/interceptors/http-interceptor.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..ebe70eff4d298ecb8ccbe5f600338761194821b7 --- /dev/null +++ b/src/app/shared/interceptors/http-interceptor.service.ts @@ -0,0 +1,29 @@ +import { Injectable } from '@angular/core'; +import { + HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, + HttpRequest, + HttpResponse +} from '@angular/common/http'; +import { Observable } from 'rxjs'; +import { environment } from 'src/environment/environment'; +import { apiConfig } from 'src/app/config/api-config'; + +@Injectable({ + providedIn: 'root' +}) +export class HttpInterceptorService implements HttpInterceptor{ + + intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { + + // if the system is not in production, change the bearer for each http request + if (!environment.production) { + const modifiedReq = req.clone({ + headers: req.headers.set('Authorization', `Bearer ${apiConfig.token}`), + }); + return next.handle(modifiedReq); + } + + // if there is nothing to intercept, pass the request. + return next.handle(req); + } +} diff --git a/src/environment/environment.ts b/src/environment/environment.ts new file mode 100644 index 0000000000000000000000000000000000000000..615da3c581113a5847011e36085dd6a8297ba87e --- /dev/null +++ b/src/environment/environment.ts @@ -0,0 +1,9 @@ +export const environment = { + // local with session + production: false, + baseURL: 'http://localhost:8888/', + backendURL: "", + isDebugMode: true + + +} \ No newline at end of file