mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Adding an authorization interceptor
This commit is contained in:
parent
26e174f3cc
commit
46158490f0
@ -1,4 +1,4 @@
|
||||
import { HttpClientModule } from '@angular/common/http';
|
||||
import {HTTP_INTERCEPTORS, HttpClientModule} from '@angular/common/http';
|
||||
import {APP_INITIALIZER, ChangeDetectorRef, NgModule} from '@angular/core';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatCardModule } from '@angular/material/card';
|
||||
@ -44,6 +44,7 @@ import {MatDialogModule} from '@angular/material/dialog';
|
||||
import {FallbackDirective} from "./misc/fallback.directive";
|
||||
import {AuthGuard} from "./misc/guards/authenticated-guard.service";
|
||||
import { AutologinComponent } from './autologin/autologin.component';
|
||||
import {AuthorizerInterceptor} from "./misc/authorizer-interceptor.service";
|
||||
|
||||
export function loadConfig(oidcConfigService: OidcConfigService)
|
||||
{
|
||||
@ -105,7 +106,12 @@ export function loadConfig(oidcConfigService: OidcConfigService)
|
||||
deps: [OidcConfigService],
|
||||
multi: true
|
||||
},
|
||||
AuthGuard
|
||||
AuthGuard,
|
||||
{
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: AuthorizerInterceptor,
|
||||
multi: true
|
||||
}
|
||||
],
|
||||
bootstrap: [AppComponent]
|
||||
})
|
||||
|
28
src/app/misc/authorizer-interceptor.service.ts
Normal file
28
src/app/misc/authorizer-interceptor.service.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import {Injectable, Injector} from '@angular/core';
|
||||
import {
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpEvent,
|
||||
HttpInterceptor
|
||||
} from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
import {OidcSecurityService} from "angular-auth-oidc-client";
|
||||
|
||||
@Injectable()
|
||||
export class AuthorizerInterceptor implements HttpInterceptor
|
||||
{
|
||||
private oidcSecurity: OidcSecurityService;
|
||||
|
||||
|
||||
constructor(private injector: Injector) {}
|
||||
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
|
||||
{
|
||||
if (this.oidcSecurity === undefined)
|
||||
this.oidcSecurity = this.injector.get(OidcSecurityService);
|
||||
let token = this.oidcSecurity.getToken();
|
||||
if (token)
|
||||
request = request.clone({setHeaders: {Authorization: "Bearer " + token}});
|
||||
return next.handle(request);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user