feat: add user creation on login (wip)

This commit is contained in:
Constantin Simonis 2025-02-13 13:16:59 +01:00
parent 44c7d8be57
commit 793f3f6834
No known key found for this signature in database
GPG key ID: 758DD9C506603183
13 changed files with 196 additions and 9 deletions

View file

@ -0,0 +1,34 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { KeycloakProfile } from 'keycloak-js';
import { async } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class UserService {
private http: HttpClient = inject(HttpClient);
public getUser(id: string) {
return this.http.get<{ keycloakId: string, username: string } | null>(`/backend/user/${id}`);
}
public createUser(id: string, username: string) {
return this.http.post<{ keycloakId: string, username: string }>('/backend/user', {
keycloakId: id,
username: username,
});
}
public async getCurrentUser(userProfile: KeycloakProfile) {
if (userProfile.id == null) {
return;
}
return await this.getUser(userProfile.id).toPromise().then(async user => {
if (user) {
return user;
}
return await this.createUser(userProfile.id ?? '', userProfile.username ?? '').toPromise();
});
}
}