Skip to content
Snippets Groups Projects
Commit d0063214 authored by Danial Hezarkhani's avatar Danial Hezarkhani
Browse files

added interceptor for http calls

parent f19921e8
No related branches found
No related tags found
1 merge request!1Features/initial setup
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 { }
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
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();
});
});
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);
}
}
export const environment = {
// local with session
production: false,
baseURL: 'http://localhost:8888/',
backendURL: "",
isDebugMode: true
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment