Files
smartguide/webapp/src/app/shared/interceptors/authentication.interceptor.ts
T
Roman 7b13db6dbf
LocationBasedServices/smartguide/pipeline/head This commit looks good
fix(auth): clean up auth v2 integration and legacy auth flow
Connect logout and change-password handling to the new auth v2 flow and
remove remaining legacy auth usage from the active frontend/backend flow.

Main changes:
- wire frontend logout and change-password handling to auth v2
- remove old auth service references from the active flow
- fix self-access handling so real_account_id == effective_account_id is not treated as impersonation
- update CredentialService token/session state handling
- update guards to validate the current session through /api/accounts/me
- update the auth interceptor for bearer tokens, public endpoint skips, and 401 recovery
- improve impersonation recovery UX with snackbar feedback and focus cleanup
- keep the registration page for now, but disable registration in CredentialService
2026-06-27 01:38:05 +02:00

87 lines
2.9 KiB
TypeScript

import { HttpErrorResponse, HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { Router } from '@angular/router';
import { MatSnackBar } from '@angular/material/snack-bar';
import { catchError, switchMap, tap, throwError } from 'rxjs';
import { CredentialService } from '../services/credential.service';
import { UiFocusService } from '../services/ui-focus.service';
export const authenticationInterceptor: HttpInterceptorFn = (req, next) => {
const url = req.url.split('?')[0];
const uiFocusService = inject(UiFocusService);
const isPublicAuthRequest =
req.method === 'POST' &&
(url.endsWith('/api/auth/v2/request-otk') ||
url.endsWith('/api/auth/v2/login') ||
url.endsWith('/api/auth/v2/access') ||
url.endsWith('/api/auth/v2/refresh'));
if (isPublicAuthRequest || url.endsWith('/institutions/autocomplete')) {
return next(req);
}
const credentialService = inject(CredentialService);
const router = inject(Router);
const snackBar = inject(MatSnackBar);
const tokenProvider = credentialService.getBearerToken();
const isLogoutRequest = url.endsWith('/api/auth/v2/logout');
return tokenProvider.pipe(
switchMap((response) => {
if (response == undefined) {
credentialService.clearLocalAuthState();
snackBar.open(
'Ihre Sitzung ist abgelaufen oder wurde beendet. Bitte melden Sie sich erneut an.',
'OK',
{ duration: 7000 },
);
void router.navigate(['/login']);
return throwError(
() => new HttpErrorResponse({ status: 403, statusText: 'Forbidden' }),
);
}
const authorizedRequest = req.clone({
headers: req.headers.set('Authorization', 'Bearer ' + response),
});
return next(authorizedRequest).pipe(
catchError((error: HttpErrorResponse) => {
if (error.status === 401 && !isLogoutRequest) {
return credentialService.handleUnauthorized().pipe(
tap((result) => {
uiFocusService.blurActiveElement();
if (result === 'stopped-impersonating') {
snackBar.open(
'Die Impersonation wurde beendet, weil die Sitzung nicht mehr gültig ist.',
'OK',
{ duration: 7000 },
);
void router.navigate(['/accounts']);
} else if (result === 'logged-out') {
snackBar.open(
'Ihre Sitzung ist abgelaufen oder wurde beendet. Bitte melden Sie sich erneut an.',
'OK',
{ duration: 7000 },
);
void router.navigate(['/login']);
}
}),
switchMap(() => throwError(() => error)),
);
}
return throwError(() => error);
}),
);
}),
);
};