feat: add user creation on login (wip)
This commit is contained in:
parent
44c7d8be57
commit
793f3f6834
13 changed files with 196 additions and 9 deletions
34
frontend/src/app/service/user.service.ts
Normal file
34
frontend/src/app/service/user.service.ts
Normal 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();
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue