Merge branch 'main' into feature/authentik
Some checks failed
CI / Get Changed Files (pull_request) Successful in 6s
CI / prettier (pull_request) Failing after 22s
CI / Checkstyle Main (pull_request) Failing after 35s
CI / eslint (pull_request) Failing after 1m41s
CI / test-build (pull_request) Successful in 1m48s

This commit is contained in:
Jan K9f 2025-04-02 16:00:01 +02:00
commit d7fe0e3965
Signed by: jank
GPG key ID: B9F475106B20F144
56 changed files with 2782 additions and 602 deletions

View file

@ -1,6 +1,6 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { catchError, EMPTY, Observable } from 'rxjs';
import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs';
import { User } from '../model/User';
@Injectable({
@ -8,16 +8,37 @@ import { User } from '../model/User';
})
export class UserService {
private http: HttpClient = inject(HttpClient);
private currentUserSubject = new BehaviorSubject<User | null>(null);
public currentUser$ = this.currentUserSubject.asObservable();
constructor() {
// Initialize with current user data
this.getCurrentUser().subscribe();
}
public getUser(id: string): Observable<User | null> {
return this.http.get<User | null>(`/backend/user/${id}`).pipe(
catchError(() => EMPTY),
tap((user) => this.currentUserSubject.next(user))
);
}
public getCurrentUser(): Observable<User | null> {
return this.http.get<User | null>('/backend/user').pipe(catchError(() => EMPTY));
return this.http.get<User | null>('/backend/user').pipe(
catchError(() => EMPTY),
tap((user) => this.currentUserSubject.next(user))
);
}
public refreshCurrentUser(): void {
this.getCurrentUser().subscribe();
}
public createUser(id: string, username: string): Observable<User> {
return this.http.post<User>('/backend/user', {
authentikId: id,
username: username,
});
}).pipe(tap((user) => this.currentUserSubject.next(user)));
}
public getOrCreateUser(profile: any): Observable<User> {
@ -26,12 +47,12 @@ export class UserService {
// Check different possible locations for the ID and username
const id = profile.info?.sub || profile['sub'];
const username = profile.info?.preferred_username || profile['preferred_username'] || profile['email'] || profile['name'];
if (!id || !username) {
console.error('Could not extract user ID or username from profile', profile);
throw new Error('Invalid user profile data');
}
console.log(`Creating user with id: ${id}, username: ${username}`);
return this.createUser(id, username);
}