diff --git a/src/app/authentication.interceptor.ts b/src/app/authentication.interceptor.ts new file mode 100644 index 0000000000000000000000000000000000000000..5924d379ac43c78ecf5fac49bbe5870ad1b680cd --- /dev/null +++ b/src/app/authentication.interceptor.ts @@ -0,0 +1,26 @@ +import { Injectable } from '@angular/core'; +import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; +import { Observable } from 'rxjs/Observable'; +import { AuthenticationService } from './authentication.service'; + +@Injectable() +export class AuthenticationInterceptor implements HttpInterceptor { + + constructor(private authenticationService: AuthenticationService) { + } + + intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { + const token = this.authenticationService.getUser().token; + + if (token) { + const cloned = req.clone({ + headers: req.headers.set('Arsnova-Auth-Token', token) + }); + + return next.handle(cloned); + } else { + return next.handle(req); + } + } + +}