Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 476b7738

Přidáno uživatelem Jakub Danek před více než 5 roky(ů)

re #37 add request interceptor for sending Basic authentication token

Zobrazit rozdíly:

webapp/src/app/app.module.ts
8 8
import {HeaderComponent} from './header/header.component';
9 9
import {MatDialogModule, MatMenuModule} from '@angular/material';
10 10
import {ProfileSettingsModule} from './profile-settings/profile-settings.module';
11
import {HttpClient, HttpClientModule} from '@angular/common/http';
11
import {HTTP_INTERCEPTORS, HttpClient, HttpClientModule} from '@angular/common/http';
12 12
import {EmployeesModule} from './employees/employees.module';
13 13
import {TranslateLoader, TranslateModule} from '@ngx-translate/core';
14 14
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
15 15
import {PageNotFoundComponent} from './page-not-found/page-not-found.component';
16 16
import {CommonModule} from '@angular/common';
17 17

  
18
import {BasicAuthInterceptor} from "./auth/basic-auth.interceptor";
19

  
18 20
@NgModule({
19 21
  declarations: [
20 22
    AppComponent,
......
40 42
    MatMenuModule,
41 43
    CommonModule
42 44
  ],
43
  providers: [],
45
  providers: [
46
    {provide: HTTP_INTERCEPTORS, useClass: BasicAuthInterceptor, multi: true}
47
  ],
44 48
  bootstrap: [AppComponent]
45 49
})
46 50
export class AppModule { }
webapp/src/app/auth/basic-auth.interceptor.ts
1
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
}

Také k dispozici: Unified diff