import { inject, Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs'; import { User } from '../model/User'; @Injectable({ providedIn: 'root', }) export class UserService { private http: HttpClient = inject(HttpClient); private currentUserSubject = new BehaviorSubject(null); public currentUser$ = this.currentUserSubject.asObservable(); constructor() { this.getCurrentUser().subscribe(); } public getUser(id: string): Observable { return this.http.get(`/backend/user/${id}`).pipe( catchError(() => EMPTY), tap((user) => this.currentUserSubject.next(user)) ); } public getCurrentUser(): Observable { return this.http.get('/backend/user').pipe( catchError(() => EMPTY), tap((user) => this.currentUserSubject.next(user)) ); } public refreshCurrentUser(): void { this.getCurrentUser().subscribe(); } public createUser(id: string, username: string): Observable { return this.http .post('/backend/user', { authentikId: id, username: username, }) .pipe(tap((user) => this.currentUserSubject.next(user))); } public getOrCreateUser(profile: Record): Observable { const info = profile['info'] as Record | undefined; const id = (info?.['sub'] as string) || (profile['sub'] as string); const username = (info?.['preferred_username'] as string) || (profile['preferred_username'] as string) || (profile['email'] as string) || (profile['name'] as string); if (!id || !username) { throw new Error('Invalid user profile data'); } return this.createUser(id, username); } }