Skip to content
Permalink
Browse files

Request interceptor added to the frontend module. A token included in…

… all API calls.
  • Loading branch information
little-pinecone committed Jan 8, 2019
1 parent 71b1338 commit e561ff512771a06404cda39e293432429a691c5e
@@ -5,7 +5,8 @@ import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CookieListComponent } from './pages/cookie-list/cookie-list.component';
import { LoginComponent } from './auth/components/login/login.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { JwtTokenInterceptor } from './auth/interceptors/jwt.token.interceptor';
import { FormsModule } from '@angular/forms';
import { FooterComponent } from './layout/footer/footer.component';
import { HeaderComponent } from './layout/header/header.component';
@@ -24,7 +25,13 @@ import { HeaderComponent } from './layout/header/header.component';
HttpClientModule,
FormsModule,
],
providers: [],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: JwtTokenInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';

import { AuthService } from '../services/auth.service';
import { HttpHandler, HttpRequest, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class JwtTokenInterceptor implements HttpInterceptor {

constructor(public auth: AuthService) {}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let interceptedRequest = request.clone({
setHeaders: {
Authorization: `Bearer ${this.auth.getToken()}`
}
});

return next.handle(interceptedRequest);
}
}

0 comments on commit e561ff5

Please sign in to comment.
You can’t perform that action at this time.