fix: fix some stuff
This commit is contained in:
parent
793f3f6834
commit
df9fa9f275
10 changed files with 132 additions and 24 deletions
|
@ -1,4 +1,4 @@
|
|||
import { Component, inject, OnInit } from '@angular/core';
|
||||
import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
|
||||
import { UserService } from '../service/user.service';
|
||||
import { KeycloakService } from 'keycloak-angular';
|
||||
import { Router } from '@angular/router';
|
||||
|
@ -8,7 +8,8 @@ import { Router } from '@angular/router';
|
|||
standalone: true,
|
||||
imports: [],
|
||||
templateUrl: './login-success.component.html',
|
||||
styleUrl: './login-success.component.css'
|
||||
styleUrl: './login-success.component.css',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class LoginSuccessComponent implements OnInit{
|
||||
private userService: UserService = inject(UserService);
|
||||
|
@ -17,7 +18,7 @@ export class LoginSuccessComponent implements OnInit{
|
|||
|
||||
async ngOnInit() {
|
||||
const userProfile = await this.keycloakService.loadUserProfile();
|
||||
const user = this.userService.getCurrentUser(userProfile);
|
||||
const user = await this.userService.getOrCreateUser(userProfile);
|
||||
sessionStorage.setItem('user', JSON.stringify(user));
|
||||
|
||||
// this.router.navigate(['']);
|
||||
|
|
4
frontend/src/app/model/User.ts
Normal file
4
frontend/src/app/model/User.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export interface User {
|
||||
keycloakId: string;
|
||||
username: string;
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { KeycloakProfile } from 'keycloak-js';
|
||||
import { async } from 'rxjs';
|
||||
import { async, Observable } from 'rxjs';
|
||||
import { User } from '../model/User';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
|
@ -9,18 +10,18 @@ import { async } from 'rxjs';
|
|||
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 getUser(id: string): Observable<User|null> {
|
||||
return this.http.get<User|null>(`/backend/user/${id}`);
|
||||
}
|
||||
|
||||
public createUser(id: string, username: string) {
|
||||
return this.http.post<{ keycloakId: string, username: string }>('/backend/user', {
|
||||
public createUser(id: string, username: string): Observable<User> {
|
||||
return this.http.post<User>('/backend/user', {
|
||||
keycloakId: id,
|
||||
username: username,
|
||||
});
|
||||
}
|
||||
|
||||
public async getCurrentUser(userProfile: KeycloakProfile) {
|
||||
public async getOrCreateUser(userProfile: KeycloakProfile) {
|
||||
if (userProfile.id == null) {
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue