123 lines
3.7 KiB
TypeScript
123 lines
3.7 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable, Signal, computed, effect, signal } from '@angular/core';
|
|
import { jwtDecode } from 'jwt-decode';
|
|
import { Observable, distinctUntilChanged, filter, from, lastValueFrom, map } from 'rxjs';
|
|
import urlcat from 'urlcat';
|
|
import { User } from '../../../../bizmatch-server/src/models/db.model';
|
|
import { JwtToken, ListingCriteria } from '../../../../bizmatch-server/src/models/main.model';
|
|
import { environment } from '../../environments/environment';
|
|
import { KeycloakService } from './keycloak.service';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class UserService {
|
|
private apiBaseUrl = environment.apiBaseUrl;
|
|
// -----------------------------
|
|
// Keycloak services
|
|
// -----------------------------
|
|
private user$ = new Observable<User>();
|
|
private user: User;
|
|
public $isLoggedIn: Signal<boolean>;
|
|
constructor(public keycloak: KeycloakService, private http: HttpClient) {
|
|
this.user$ = from(this.keycloak.getToken()).pipe(
|
|
filter(t => !!t),
|
|
distinctUntilChanged(),
|
|
map(t => this.map2User(t)),
|
|
// tap(u => {
|
|
// logger.info('Logged in user:', u);
|
|
// this.analyticsService.identify(u);
|
|
// }),
|
|
);
|
|
this.$isLoggedIn = signal(false);
|
|
this.$isLoggedIn = computed(() => {
|
|
return keycloak.isLoggedIn();
|
|
});
|
|
|
|
effect(async () => {
|
|
if (this.$isLoggedIn()) {
|
|
this.updateTokenDetails();
|
|
} else {
|
|
this.user = null;
|
|
}
|
|
});
|
|
}
|
|
|
|
private async refreshToken(): Promise<void> {
|
|
try {
|
|
await this.keycloak.updateToken(10); // Versuche, den Token zu erneuern
|
|
await this.updateTokenDetails(); // Aktualisiere den Token und seine Details
|
|
} catch (error) {
|
|
console.error('Fehler beim Token-Refresh', error);
|
|
}
|
|
}
|
|
private async updateTokenDetails(): Promise<void> {
|
|
const token = await this.keycloak.getToken();
|
|
this.user = this.map2User(token);
|
|
}
|
|
|
|
private map2User(jwt: string): User {
|
|
const token = jwtDecode<JwtToken>(jwt);
|
|
return {
|
|
id: token.user_id,
|
|
firstname: token.given_name,
|
|
lastname: token.family_name,
|
|
email: token.email,
|
|
};
|
|
}
|
|
|
|
isLoggedIn(): boolean {
|
|
return this.$isLoggedIn();
|
|
}
|
|
getUser(): User {
|
|
return this.user;
|
|
}
|
|
getUserObservable(): Observable<User> {
|
|
return this.user$;
|
|
}
|
|
async getId(): Promise<string> {
|
|
if (sessionStorage.getItem('USERID')) {
|
|
return sessionStorage.getItem('USERID');
|
|
} else {
|
|
const user = await this.getByMail(this.user.email);
|
|
sessionStorage.setItem('USERID', user.id);
|
|
return user.id;
|
|
}
|
|
}
|
|
logout() {
|
|
sessionStorage.removeItem('USERID');
|
|
this.keycloak.logout(window.location.origin + '/home');
|
|
}
|
|
async login(url: string) {
|
|
await this.keycloak.login({
|
|
redirectUri: url,
|
|
});
|
|
}
|
|
getUserRoles() {
|
|
return this.keycloak.getUserRoles(true);
|
|
}
|
|
hasAdminRole() {
|
|
return this.keycloak.getUserRoles(true).includes('ADMIN');
|
|
}
|
|
register(url: string) {
|
|
this.keycloak.register({ redirectUri: url });
|
|
}
|
|
|
|
// -----------------------------
|
|
// DB services
|
|
// -----------------------------
|
|
async save(user: User): Promise<User> {
|
|
return await lastValueFrom(this.http.post<User>(`${this.apiBaseUrl}/bizmatch/user`, user));
|
|
}
|
|
async getById(id: string): Promise<User> {
|
|
return await lastValueFrom(this.http.get<User>(`${this.apiBaseUrl}/bizmatch/user/${id}`));
|
|
}
|
|
async getByMail(mail: string): Promise<User> {
|
|
const url = urlcat(`${this.apiBaseUrl}/bizmatch/user`, { mail });
|
|
return await lastValueFrom(this.http.get<User>(url));
|
|
}
|
|
async search(criteria?: ListingCriteria) {
|
|
return await lastValueFrom(this.http.post<User[]>(`${this.apiBaseUrl}/bizmatch/user/search`, criteria));
|
|
}
|
|
}
|