Skip to content

Commit

Permalink
ng update and interceptor as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofstetter Benjamin (extern) committed Jun 3, 2024
1 parent d3a4497 commit 5bb3a5e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 350 deletions.
357 changes: 74 additions & 283 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"private": true,
"dependencies": {
"@angular/animations": "^18.0.1",
"@angular/cdk": "^17.3.9",
"@angular/cdk": "^18.0.1",
"@angular/common": "^18.0.1",
"@angular/compiler": "^18.0.1",
"@angular/core": "^18.0.1",
Expand Down Expand Up @@ -41,11 +41,11 @@
},
"devDependencies": {
"@angular-devkit/build-angular": "^18.0.2",
"@angular-eslint/builder": "17.4.1",
"@angular-eslint/eslint-plugin": "17.4.1",
"@angular-eslint/eslint-plugin-template": "17.4.1",
"@angular-eslint/schematics": "17.4.1",
"@angular-eslint/template-parser": "17.4.1",
"@angular-eslint/builder": "18.0.1",
"@angular-eslint/eslint-plugin": "18.0.1",
"@angular-eslint/eslint-plugin-template": "18.0.1",
"@angular-eslint/schematics": "18.0.1",
"@angular-eslint/template-parser": "18.0.1",
"@angular/cli": "^18.0.2",
"@angular/compiler-cli": "^18.0.1",
"@types/clownface": "^2.0.7",
Expand Down
11 changes: 5 additions & 6 deletions projects/blueprint/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import {
} from '@angular/router';

import { routes } from './app.routes';
import { APP_INITIALIZER, importProvidersFrom } from '@angular/core';
import { APP_INITIALIZER } from '@angular/core';

import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { provideAnimations } from '@angular/platform-browser/animations';
import { ConfigService } from './core/service/config/config.service';
import { authInterceptor } from '@blueprint/http-interceptor/auth-interceptor/auth-interceptor.function';

import { AuthInterceptor } from '@blueprint/http-interceptor/auth-interceptor/auth-interceptor.service';

function initializeAppFactory(configService: ConfigService): () => void {
return () => configService.fetchConfig()
}

export const appConfig: ApplicationConfig = {
providers: [
importProvidersFrom(HttpClientModule),
provideHttpClient(withInterceptors([authInterceptor])),
provideRouter(routes,
withInMemoryScrolling({ anchorScrolling: 'enabled', scrollPositionRestoration: 'enabled' }),
withComponentInputBinding()
Expand All @@ -29,8 +29,7 @@ export const appConfig: ApplicationConfig = {
useFactory: initializeAppFactory,
deps: [ConfigService],
multi: true
},
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
}

],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { inject } from '@angular/core';
import { HttpRequest, HttpEvent, HttpHandlerFn } from '@angular/common/http';
import { Observable, catchError, throwError } from 'rxjs';
import { Router } from '@angular/router';
import { LibraryConfigurationService } from '@blueprint/service/library-configuration/library-configuration.service';
import { AuthService } from '@blueprint/service/auth/auth.service';

/**
* An HTTP interceptor that adds basic auth credentials to requests to the library configuration service.
*/


/**
* Intercepts HTTP requests and adds basic auth credentials to requests to the library configuration service.
* @param req The HTTP request.
* @param next The HTTP handler.
* @returns An observable of the HTTP event.
*/
export function authInterceptor(req: HttpRequest<unknown>, next: HttpHandlerFn): Observable<HttpEvent<any>> {
const libraryConfig = inject(LibraryConfigurationService);
const authService = inject(AuthService);
const router = inject(Router);
// If the request is for the sparql endpoint, add the basic auth credentials.
if (req.url.includes(libraryConfig.endpointUrl)) {
// Create the authorization header by base64-encoding the username and password separated by a colon.
const credentials = authService.getCredentials();
const authHeader = `Basic ${btoa(`${credentials ? credentials.username : ''}:${credentials ? credentials.password : ''}`)}`;

// Clone the original request and add the authorization header to the headers.
const authReq = req.clone({
headers: req.headers.set('Authorization', authHeader)
});

// Pass the modified request to the next handler in the chain.
return next(authReq).pipe(
catchError(error => {
// If the response returns an HTTP 401 status code (Unauthorized), navigate to the login page.
if (error.status === 401) {
router.navigate(['/login']);
}
// Rethrow the error.
return throwError(() => new Error(error.message))
})
);
} else {
// If the request is not for the library configuration service, pass the original request to the next handler in the chain.
return next(req);
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion projects/blueprint/src/app/core/http-interceptor/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './auth-interceptor/auth-interceptor.service'
export * from './auth-interceptor/auth-interceptor.function'
2 changes: 1 addition & 1 deletion projects/blueprint/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</head>

<body>
<bp-root />
<bp-root></bp-root>
<noscript>Please enable JavaScript to continue using this application.</noscript>
</body>

Expand Down

0 comments on commit 5bb3a5e

Please sign in to comment.