fix: fix some stuff
This commit is contained in:
parent
793f3f6834
commit
df9fa9f275
10 changed files with 132 additions and 24 deletions
|
@ -1,25 +1,55 @@
|
||||||
package de.szut.casino.user;
|
package de.szut.casino.user;
|
||||||
|
|
||||||
import de.szut.casino.user.dto.CreateUserDto;
|
import de.szut.casino.user.dto.CreateUserDto;
|
||||||
|
import de.szut.casino.user.dto.GetUserDto;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/user/{id}")
|
@GetMapping("/user/{id}")
|
||||||
public ResponseEntity<?> getUser(@PathVariable String id) {
|
public ResponseEntity<?> getUser(@PathVariable String id) {
|
||||||
|
if (id == null || !userService.exists(id)) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
return ResponseEntity.ok(userService.getUser(id));
|
return ResponseEntity.ok(userService.getUser(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/user")
|
@PostMapping("/user")
|
||||||
public ResponseEntity<?> createUser(@RequestBody @Valid CreateUserDto userData) {
|
public ResponseEntity<?> createUser(@RequestBody @Valid CreateUserDto userData) {
|
||||||
|
if (userService.exists(userData.getKeycloakId())) {
|
||||||
|
|
||||||
|
return this.redirect("/user/" + userData.getKeycloakId());
|
||||||
|
}
|
||||||
|
|
||||||
return ResponseEntity.ok(userService.createUser(userData));
|
return ResponseEntity.ok(userService.createUser(userData));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/user")
|
||||||
|
public ResponseEntity<GetUserDto> getCurrentUser(@RequestHeader("Authorization") String token) {
|
||||||
|
GetUserDto userData = userService.getCurrentUser(token);
|
||||||
|
|
||||||
|
if (userData == null) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.ok(userData);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<Object> redirect(String route) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.add("Location", route);
|
||||||
|
|
||||||
|
return new ResponseEntity<>(headers, HttpStatus.FOUND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package de.szut.casino.user;
|
package de.szut.casino.user;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.Id;
|
import jakarta.persistence.Id;
|
||||||
|
@ -13,6 +14,8 @@ public class UserEntity {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private Long id;
|
private Long id;
|
||||||
|
@Column(unique = true)
|
||||||
private String keycloakId;
|
private String keycloakId;
|
||||||
private String username;
|
private String username;
|
||||||
|
private float balance;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package de.szut.casino.user;
|
||||||
|
|
||||||
|
import de.szut.casino.user.dto.CreateUserDto;
|
||||||
|
import de.szut.casino.user.dto.GetUserDto;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserMappingService {
|
||||||
|
public GetUserDto mapToGetUserDto(UserEntity user) {
|
||||||
|
return new GetUserDto(user.getKeycloakId(), user.getUsername(), user.getBalance());
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity mapToUserEntity(CreateUserDto createUserDto) {
|
||||||
|
UserEntity user = new UserEntity();
|
||||||
|
user.setKeycloakId(createUserDto.getKeycloakId());
|
||||||
|
user.setUsername(createUserDto.getUsername());
|
||||||
|
user.setBalance(0);
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,9 +8,8 @@ import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
||||||
boolean existsByUsername(String username);
|
|
||||||
|
|
||||||
@Query("SELECT u FROM UserEntity u WHERE u.keycloakId = ?1")
|
@Query("SELECT u FROM UserEntity u WHERE u.keycloakId = ?1")
|
||||||
Optional<UserEntity> findOneByKeycloakId(String keycloakId);
|
Optional<UserEntity> findOneByKeycloakId(String keycloakId);
|
||||||
|
|
||||||
|
boolean existsByKeycloakId(String keycloakId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,21 @@ package de.szut.casino.user;
|
||||||
|
|
||||||
import de.szut.casino.user.dto.CreateUserDto;
|
import de.szut.casino.user.dto.CreateUserDto;
|
||||||
import de.szut.casino.user.dto.GetUserDto;
|
import de.szut.casino.user.dto.GetUserDto;
|
||||||
|
import de.szut.casino.user.dto.KeycloakUserDto;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@ -12,10 +24,14 @@ public class UserService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate http;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserMappingService mappingService;
|
||||||
|
|
||||||
public UserEntity createUser(CreateUserDto createUserDto) {
|
public UserEntity createUser(CreateUserDto createUserDto) {
|
||||||
UserEntity user = new UserEntity();
|
UserEntity user = mappingService.mapToUserEntity(createUserDto);
|
||||||
user.setUsername(createUserDto.getUsername());
|
|
||||||
user.setKeycloakId(createUserDto.getKeycloakId());
|
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
@ -23,15 +39,31 @@ public class UserService {
|
||||||
|
|
||||||
public GetUserDto getUser(String keycloakId) {
|
public GetUserDto getUser(String keycloakId) {
|
||||||
Optional<UserEntity> user = this.userRepository.findOneByKeycloakId(keycloakId);
|
Optional<UserEntity> user = this.userRepository.findOneByKeycloakId(keycloakId);
|
||||||
if (user.isPresent()) {
|
return user.map(userEntity -> mappingService.mapToGetUserDto(userEntity)).orElse(null);
|
||||||
System.out.println(user.get());
|
|
||||||
GetUserDto getUserDto = new GetUserDto();
|
|
||||||
getUserDto.setKeycloakId(user.get().getKeycloakId());
|
|
||||||
getUserDto.setUsername(user.get().getUsername());
|
|
||||||
return getUserDto;
|
|
||||||
}
|
|
||||||
System.out.println("User not found: "+keycloakId);
|
|
||||||
|
|
||||||
return null;
|
}
|
||||||
|
|
||||||
|
public GetUserDto getCurrentUser(String token) {
|
||||||
|
KeycloakUserDto userData = getKeycloakUserInfo(token);
|
||||||
|
|
||||||
|
if (userData == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Optional<UserEntity> user = this.userRepository.findOneByKeycloakId(userData.getSub());
|
||||||
|
|
||||||
|
return user.map(userEntity -> mappingService.mapToGetUserDto(userEntity)).orElse(null);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private KeycloakUserDto getKeycloakUserInfo(String token) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("Authorization", token);
|
||||||
|
ResponseEntity<KeycloakUserDto> response = this.http.exchange("http://localhost:9090/realms/LF12/protocol/openid-connect/userinfo", HttpMethod.GET, new HttpEntity<>(headers), KeycloakUserDto.class);
|
||||||
|
|
||||||
|
return response.getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean exists(String keycloakId) {
|
||||||
|
return userRepository.existsByKeycloakId(keycloakId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,4 +12,5 @@ import lombok.Setter;
|
||||||
public class GetUserDto {
|
public class GetUserDto {
|
||||||
private String keycloakId;
|
private String keycloakId;
|
||||||
private String username;
|
private String username;
|
||||||
|
private float balance;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
package de.szut.casino.user.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class KeycloakUserDto {
|
||||||
|
private String sub;
|
||||||
|
private String preferred_username;
|
||||||
|
}
|
|
@ -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 { UserService } from '../service/user.service';
|
||||||
import { KeycloakService } from 'keycloak-angular';
|
import { KeycloakService } from 'keycloak-angular';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
|
@ -8,7 +8,8 @@ import { Router } from '@angular/router';
|
||||||
standalone: true,
|
standalone: true,
|
||||||
imports: [],
|
imports: [],
|
||||||
templateUrl: './login-success.component.html',
|
templateUrl: './login-success.component.html',
|
||||||
styleUrl: './login-success.component.css'
|
styleUrl: './login-success.component.css',
|
||||||
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export class LoginSuccessComponent implements OnInit{
|
export class LoginSuccessComponent implements OnInit{
|
||||||
private userService: UserService = inject(UserService);
|
private userService: UserService = inject(UserService);
|
||||||
|
@ -17,7 +18,7 @@ export class LoginSuccessComponent implements OnInit{
|
||||||
|
|
||||||
async ngOnInit() {
|
async ngOnInit() {
|
||||||
const userProfile = await this.keycloakService.loadUserProfile();
|
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));
|
sessionStorage.setItem('user', JSON.stringify(user));
|
||||||
|
|
||||||
// this.router.navigate(['']);
|
// 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 { inject, Injectable } from '@angular/core';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import { KeycloakProfile } from 'keycloak-js';
|
import { KeycloakProfile } from 'keycloak-js';
|
||||||
import { async } from 'rxjs';
|
import { async, Observable } from 'rxjs';
|
||||||
|
import { User } from '../model/User';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
|
@ -9,18 +10,18 @@ import { async } from 'rxjs';
|
||||||
export class UserService {
|
export class UserService {
|
||||||
private http: HttpClient = inject(HttpClient);
|
private http: HttpClient = inject(HttpClient);
|
||||||
|
|
||||||
public getUser(id: string) {
|
public getUser(id: string): Observable<User|null> {
|
||||||
return this.http.get<{ keycloakId: string, username: string } | null>(`/backend/user/${id}`);
|
return this.http.get<User|null>(`/backend/user/${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
public createUser(id: string, username: string) {
|
public createUser(id: string, username: string): Observable<User> {
|
||||||
return this.http.post<{ keycloakId: string, username: string }>('/backend/user', {
|
return this.http.post<User>('/backend/user', {
|
||||||
keycloakId: id,
|
keycloakId: id,
|
||||||
username: username,
|
username: username,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getCurrentUser(userProfile: KeycloakProfile) {
|
public async getOrCreateUser(userProfile: KeycloakProfile) {
|
||||||
if (userProfile.id == null) {
|
if (userProfile.id == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue