Fix: Auth Bcakground reload after auth
All checks were successful
Quality Check / Linting (push) Successful in 29s

This commit is contained in:
Dominik Säume 2025-01-22 10:04:14 +01:00
parent 28bb4e220e
commit 523e5f4eee
3 changed files with 71 additions and 67 deletions

View file

@ -1,17 +1,17 @@
import {provideHttpClient} from '@angular/common/http'; import { provideHttpClient } from '@angular/common/http';
import {ApplicationConfig, provideZoneChangeDetection} from '@angular/core'; import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field'; import { MAT_FORM_FIELD_DEFAULT_OPTIONS } from '@angular/material/form-field';
import {provideAnimationsAsync} from '@angular/platform-browser/animations/async'; import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import {provideRouter} from '@angular/router'; import { provideRouter } from '@angular/router';
import {routes} from '@app/app.routes'; import { routes } from '@app/app.routes';
import {OpenAPI} from '@core/ems/core/OpenAPI'; import { OpenAPI } from '@core/ems/core/OpenAPI';
import {LogLevel, provideAuth} from 'angular-auth-oidc-client'; import { LogLevel, provideAuth } from 'angular-auth-oidc-client';
OpenAPI.BASE = 'http://localhost:8080'; OpenAPI.BASE = 'http://localhost:8080';
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [ providers: [
provideZoneChangeDetection({eventCoalescing: true}), provideZoneChangeDetection({ eventCoalescing: true }),
provideAnimationsAsync(), provideAnimationsAsync(),
provideRouter(routes), provideRouter(routes),
provideHttpClient(), provideHttpClient(),

View file

@ -1,28 +1,26 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CanActivate, GuardResult, MaybeAsync, RedirectCommand, Router } from '@angular/router'; import { CanActivate, GuardResult, MaybeAsync, RedirectCommand, Router } from '@angular/router';
import UserData from '@core/auth/UserData'; import UserData from '@core/auth/UserData';
import { OpenAPI } from '@core/ems';
import { OidcSecurityService } from 'angular-auth-oidc-client'; import { OidcSecurityService } from 'angular-auth-oidc-client';
import { Observable } from 'rxjs'; import { BehaviorSubject, Observable } from 'rxjs';
import { OpenAPI } from '../ems';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class AuthService implements CanActivate { export class AuthService implements CanActivate {
public $user: Observable<UserData | undefined>; public $user: BehaviorSubject<UserData | undefined>;
constructor(private readonly oidcSecurityService: OidcSecurityService, private router: Router) { constructor(private readonly oidcSecurityService: OidcSecurityService, private router: Router) {
this.$user = new Observable((publish) => { this.$user = new BehaviorSubject<UserData | undefined>(undefined);
this.oidcSecurityService.checkAuth().subscribe(({ isAuthenticated, userData }) => { this.oidcSecurityService.checkAuth().subscribe(({ isAuthenticated, userData, accessToken }) => {
publish.next(isAuthenticated ? { OpenAPI.TOKEN = accessToken;
username: userData.preferred_username, const isLoggedIn = isAuthenticated && userData != null && accessToken != '';
verified: userData.email_verified this.$user.next(isLoggedIn ? {
} : undefined); username: userData.preferred_username,
}); verified: userData.email_verified
} : undefined);
}); });
this.oidcSecurityService.getAccessToken().subscribe(token => OpenAPI.TOKEN = token);
} }
canActivate(): MaybeAsync<GuardResult> { canActivate(): MaybeAsync<GuardResult> {

View file

@ -1,20 +1,20 @@
import {AsyncPipe} from '@angular/common'; import { AsyncPipe } from '@angular/common';
import {Component} from '@angular/core'; import { Component, OnInit } from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms'; import { ReactiveFormsModule } from '@angular/forms';
import {MatButtonModule} from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import {MatDialog} from '@angular/material/dialog'; import { MatDialog } from '@angular/material/dialog';
import {MatFormFieldModule} from '@angular/material/form-field'; import { MatFormFieldModule } from '@angular/material/form-field';
import {MatIcon} from '@angular/material/icon'; import { MatIcon } from '@angular/material/icon';
import {MatInputModule} from '@angular/material/input'; import { MatInputModule } from '@angular/material/input';
import {MatSelectModule} from '@angular/material/select'; import { MatSelectModule } from '@angular/material/select';
import {MatTableDataSource, MatTableModule} from '@angular/material/table'; import { MatTableDataSource, MatTableModule } from '@angular/material/table';
import {RouterLink} from '@angular/router'; import { RouterLink } from '@angular/router';
import {DeleteModelComponent} from '@app/delete-model/delete-model.component'; import { DeleteModelComponent } from '@app/delete-model/delete-model.component';
import Filter from '@app/views/dashboard/Filter'; import Filter from '@app/views/dashboard/Filter';
import {AuthService} from '@core/auth/auth.service'; import { AuthService } from '@core/auth/auth.service';
import {Employee, EmployeeService, Qualification, QualificationService} from '@core/ems'; import { Employee, EmployeeService, Qualification, QualificationService } from '@core/ems';
import {NotificationService, NotificationType} from '@core/notification/notification.service'; import { NotificationService, NotificationType } from '@core/notification/notification.service';
import {Observable} from 'rxjs'; import { Observable, of } from 'rxjs';
@Component({ @Component({
selector: 'app-dashboard', selector: 'app-dashboard',
@ -22,13 +22,13 @@ import {Observable} from 'rxjs';
templateUrl: './dashboard.component.html', templateUrl: './dashboard.component.html',
styleUrl: './dashboard.component.scss' styleUrl: './dashboard.component.scss'
}) })
export class DashboardComponent { export class DashboardComponent implements OnInit {
selectedQualificationFilter: Qualification | undefined; selectedQualificationFilter: Qualification | undefined;
fuzzyFilter: string = ''; fuzzyFilter: string = '';
employeeDataSource: MatTableDataSource<Employee> = new MatTableDataSource<Employee>([]); employeeDataSource: MatTableDataSource<Employee> = new MatTableDataSource<Employee>([]);
employeesDisplayedColumns = ['id', 'first-name', 'last-name', 'skills', 'actions']; employeesDisplayedColumns = ['id', 'first-name', 'last-name', 'skills', 'actions'];
$qualifications: Observable<Array<Qualification> | undefined>; $qualifications: Observable<Array<Qualification> | undefined> = of();
constructor( constructor(
protected auth: AuthService, protected auth: AuthService,
@ -36,35 +36,41 @@ export class DashboardComponent {
private qualificationService: QualificationService, private qualificationService: QualificationService,
private dialog: MatDialog, private dialog: MatDialog,
private notifications: NotificationService private notifications: NotificationService
) { ) { }
this.$qualifications = this.qualificationService.getAllQualifications(); ngOnInit(): void {
this.employeeService.getAllEmployees().subscribe((employees) => { this.auth.$user.subscribe((user) => {
this.employeeDataSource = new MatTableDataSource(employees); if (user === undefined) {
this.employeeDataSource.filterPredicate = (employee: Employee, rawFilter: string): boolean => { return;
const filter = JSON.parse(rawFilter) as Filter; }
if (filter.fuzzy == '' && filter.qualification == undefined) { this.$qualifications = this.qualificationService.getAllQualifications();
this.employeeService.getAllEmployees().subscribe((employees) => {
this.employeeDataSource = new MatTableDataSource(employees);
this.employeeDataSource.filterPredicate = (employee: Employee, rawFilter: string): boolean => {
const filter = JSON.parse(rawFilter) as Filter;
if (filter.fuzzy == '' && filter.qualification == undefined) {
return true;
}
if (
filter.qualification != undefined
&& !employee.skillSet.map((skill) => skill.id).includes(filter.qualification.id)
) {
return false;
}
if (
filter.fuzzy != ''
&& !employee.id.toString().includes(filter.fuzzy)
&& !employee.firstName.toLowerCase().includes(filter.fuzzy)
&& !employee.lastName.toLowerCase().includes(filter.fuzzy)
) {
return false;
}
return true; return true;
} };
if (
filter.qualification != undefined
&& !employee.skillSet.map((skill) => skill.id).includes(filter.qualification.id)
) {
return false;
}
if (
filter.fuzzy != ''
&& !employee.id.toString().includes(filter.fuzzy)
&& !employee.firstName.toLowerCase().includes(filter.fuzzy)
&& !employee.lastName.toLowerCase().includes(filter.fuzzy)
) {
return false;
}
return true;
};
});
}); });
} }
@ -82,7 +88,7 @@ export class DashboardComponent {
if (!accepted) { if (!accepted) {
return; return;
} }
this.employeeService.deleteEmployee({id: employee.id}).subscribe(() => { this.employeeService.deleteEmployee({ id: employee.id }).subscribe(() => {
const data = this.employeeDataSource.data; const data = this.employeeDataSource.data;
const i = data.indexOf(employee); const i = data.indexOf(employee);
if (i != -1) { if (i != -1) {