Expose safe public app settings via dedicated endpoint
This commit is contained in:
committed by
Aditya Chandel
parent
f5e58080ea
commit
7cea853756
@@ -11,6 +11,7 @@ import {AppSettingsService} from '../../service/app-settings.service';
|
||||
import {AppSettings} from '../../model/app-settings.model';
|
||||
import {Observable} from 'rxjs';
|
||||
import {filter, take} from 'rxjs/operators';
|
||||
import {PublicAppSettings, PublicAppSettingService} from '../../../public-app-settings.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
@@ -33,20 +34,20 @@ export class LoginComponent implements OnInit {
|
||||
|
||||
private authService = inject(AuthService);
|
||||
private oAuthService = inject(OAuthService);
|
||||
private appSettingsService = inject(AppSettingsService);
|
||||
private appSettingsService = inject(PublicAppSettingService);
|
||||
private router = inject(Router);
|
||||
|
||||
appSettings$: Observable<AppSettings | null> = this.appSettingsService.appSettings$;
|
||||
publicAppSettings$: Observable<PublicAppSettings | null> = this.appSettingsService.publicAppSettings$;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.appSettings$
|
||||
this.publicAppSettings$
|
||||
.pipe(
|
||||
filter(settings => settings != null),
|
||||
take(1)
|
||||
)
|
||||
.subscribe(settings => {
|
||||
this.oidcEnabled = settings!.oidcEnabled;
|
||||
this.oidcName = settings!.oidcProviderDetails?.providerName || 'OIDC';
|
||||
.subscribe(publicSettings => {
|
||||
this.oidcEnabled = publicSettings!.oidcEnabled;
|
||||
this.oidcName = publicSettings!.oidcProviderDetails?.providerName || 'OIDC';
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,44 +1,53 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {BehaviorSubject, Observable, of} from 'rxjs';
|
||||
import {AppSettings} from '../model/app-settings.model';
|
||||
import {API_CONFIG} from '../../config/api-config';
|
||||
import {catchError, map} from 'rxjs/operators';
|
||||
import {catchError, finalize, shareReplay, tap} from 'rxjs/operators';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@Injectable({providedIn: 'root'})
|
||||
export class AppSettingsService {
|
||||
private http = inject(HttpClient);
|
||||
private readonly apiUrl = `${API_CONFIG.BASE_URL}/api/v1/settings`;
|
||||
|
||||
private loading$: Observable<AppSettings> | null = null;
|
||||
private appSettingsSubject = new BehaviorSubject<AppSettings | null>(null);
|
||||
appSettings$ = this.appSettingsSubject.asObservable();
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
this.loadAppSettings();
|
||||
}
|
||||
|
||||
loadAppSettings(): void {
|
||||
this.http.get<AppSettings>(this.apiUrl).subscribe({
|
||||
next: (settings: AppSettings) => {
|
||||
this.appSettingsSubject.next(settings);
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Error loading app settings:', error);
|
||||
this.appSettingsSubject.next(null);
|
||||
appSettings$ = this.appSettingsSubject.asObservable().pipe(
|
||||
tap(state => {
|
||||
if (!state && !this.loading$) {
|
||||
this.loading$ = this.fetchAppSettings().pipe(
|
||||
shareReplay(1),
|
||||
finalize(() => (this.loading$ = null))
|
||||
);
|
||||
this.loading$.subscribe();
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
private fetchAppSettings(): Observable<AppSettings> {
|
||||
return this.http.get<AppSettings>(this.apiUrl).pipe(
|
||||
tap(settings => this.appSettingsSubject.next(settings)),
|
||||
catchError(err => {
|
||||
console.error('Error loading app settings:', err);
|
||||
this.appSettingsSubject.next(null);
|
||||
throw err;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
saveSettings(settings: { key: string, newValue: any }[]): Observable<void> {
|
||||
saveSettings(settings: { key: string; newValue: any }[]): Observable<void> {
|
||||
const payload = settings.map(setting => ({
|
||||
name: setting.key,
|
||||
value: setting.newValue
|
||||
}));
|
||||
|
||||
return this.http.put<void>(this.apiUrl, payload).pipe(
|
||||
map(() => {
|
||||
this.loadAppSettings();
|
||||
tap(() => {
|
||||
this.loading$ = this.fetchAppSettings().pipe(
|
||||
shareReplay(1),
|
||||
finalize(() => (this.loading$ = null))
|
||||
);
|
||||
this.loading$.subscribe();
|
||||
}),
|
||||
catchError(err => {
|
||||
console.error('Error saving settings:', err);
|
||||
|
||||
Reference in New Issue
Block a user