Merge branch 'main' into feature/authentik
Some checks failed
Some checks failed
This commit is contained in:
commit
d7fe0e3965
56 changed files with 2782 additions and 602 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Reference in a new issue