1 |
476b7738
|
Jakub Danek
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
|
3 |
|
|
import {Observable} from 'rxjs';
|
4 |
|
|
|
5 |
|
|
import {ProfileService} from '../services/util/profile.service';
|
6 |
|
|
|
7 |
|
|
/**
|
8 |
|
|
* Adds authentication token to each request.
|
9 |
|
|
*/
|
10 |
|
|
@Injectable()
|
11 |
|
|
export class BasicAuthInterceptor implements HttpInterceptor {
|
12 |
|
|
constructor(private profileService: ProfileService) {
|
13 |
|
|
}
|
14 |
|
|
|
15 |
|
|
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
16 |
|
|
// add authorization header with basic auth credentials if available
|
17 |
|
|
const currentUser = window.btoa(this.profileService.currentUserValue + ":");
|
18 |
|
|
if (currentUser) {
|
19 |
|
|
request = request.clone({
|
20 |
|
|
withCredentials: true,
|
21 |
|
|
setHeaders: {
|
22 |
|
|
authorization: `Basic ${currentUser}`
|
23 |
|
|
}
|
24 |
|
|
});
|
25 |
|
|
}
|
26 |
|
|
|
27 |
|
|
return next.handle(request);
|
28 |
|
|
}
|
29 |
|
|
}
|