Compare commits
3 Commits
428c33b000
...
cdd9a4bd21
Author | SHA1 | Date | |
---|---|---|---|
cdd9a4bd21 | |||
429d397ddd | |||
1fc38de35e |
@ -17,15 +17,16 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
distribution: "temurin"
|
distribution: "temurin"
|
||||||
java-version: "22"
|
java-version: "22"
|
||||||
- name: "Cache Gradle dependencies"
|
|
||||||
uses: actions/cache@v3
|
- uses: actions/cache@v3
|
||||||
|
working-directory: ./backend
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.gradle/caches
|
~/.gradle/caches
|
||||||
~/.gradle/wrapper
|
~/.gradle/wrapper
|
||||||
key: gradle-${{ runner.os }}-common
|
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
gradle-${{ runner.os }}-
|
${{ runner.os }}-gradle-
|
||||||
- name: "Prepare Gradle"
|
- name: "Prepare Gradle"
|
||||||
working-directory: ./backend
|
working-directory: ./backend
|
||||||
run: gradle clean
|
run: gradle clean
|
||||||
@ -46,14 +47,6 @@ jobs:
|
|||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Install bun
|
- name: Install bun
|
||||||
uses: oven-sh/setup-bun@v2
|
uses: oven-sh/setup-bun@v2
|
||||||
- uses: actions/cache@v3
|
|
||||||
working-directory: ./frontend
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
frontend/node_modules/
|
|
||||||
key: ${{ runner.os }}-bun-
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-bun-
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
cd frontend
|
cd frontend
|
||||||
@ -73,14 +66,6 @@ jobs:
|
|||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Install bun
|
- name: Install bun
|
||||||
uses: oven-sh/setup-bun@v2
|
uses: oven-sh/setup-bun@v2
|
||||||
- uses: actions/cache@v3
|
|
||||||
working-directory: ./frontend
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
frontend/node_modules/
|
|
||||||
key: ${{ runner.os }}-bun-
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-bun-
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
cd frontend
|
cd frontend
|
||||||
@ -100,22 +85,6 @@ jobs:
|
|||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Install bun
|
- name: Install bun
|
||||||
uses: oven-sh/setup-bun@v2
|
uses: oven-sh/setup-bun@v2
|
||||||
- uses: actions/cache@v3
|
|
||||||
working-directory: ./frontend
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
frontend/node_modules/
|
|
||||||
key: ${{ runner.os }}-bun-
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-bun-
|
|
||||||
- uses: actions/cache@v3
|
|
||||||
working-directory: ./frontend
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
frontend/dist/
|
|
||||||
key: ${{ runner.os }}-dist-
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-dist-
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
cd frontend
|
cd frontend
|
||||||
|
@ -1,62 +0,0 @@
|
|||||||
package de.szut.casino.user;
|
|
||||||
|
|
||||||
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.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import de.szut.casino.user.dto.CreateUserDto;
|
|
||||||
import de.szut.casino.user.dto.GetUserDto;
|
|
||||||
import jakarta.validation.Valid;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@RestController
|
|
||||||
public class UserController {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserService userService;
|
|
||||||
|
|
||||||
@GetMapping("/user/{id}")
|
|
||||||
public ResponseEntity<?> getUser(@PathVariable String id) {
|
|
||||||
if (id == null || !userService.exists(id)) {
|
|
||||||
return ResponseEntity.notFound().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseEntity.ok(userService.getUser(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/user")
|
|
||||||
public ResponseEntity<?> createUser(@RequestBody @Valid CreateUserDto userData) {
|
|
||||||
if (userService.exists(userData.getKeycloakId())) {
|
|
||||||
|
|
||||||
return this.redirect("/user/" + userData.getKeycloakId());
|
|
||||||
}
|
|
||||||
|
|
||||||
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,30 +0,0 @@
|
|||||||
package de.szut.casino.user;
|
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Setter
|
|
||||||
@Getter
|
|
||||||
@Entity
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class UserEntity {
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
private Long id;
|
|
||||||
@Column(unique = true)
|
|
||||||
private String keycloakId;
|
|
||||||
private String username;
|
|
||||||
private float balance;
|
|
||||||
|
|
||||||
public UserEntity(String keycloakId, String username, float balance) {
|
|
||||||
this.keycloakId = keycloakId;
|
|
||||||
this.username = username;
|
|
||||||
this.balance = balance;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
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) {
|
|
||||||
return new UserEntity(createUserDto.getKeycloakId(), createUserDto.getUsername(), 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
|||||||
package de.szut.casino.user;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.data.jpa.repository.Query;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
|
||||||
@Query("SELECT u FROM UserEntity u WHERE u.keycloakId = ?1")
|
|
||||||
Optional<UserEntity> findOneByKeycloakId(String keycloakId);
|
|
||||||
|
|
||||||
boolean existsByKeycloakId(String keycloakId);
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
package de.szut.casino.user;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpEntity;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.HttpMethod;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
import de.szut.casino.user.dto.CreateUserDto;
|
|
||||||
import de.szut.casino.user.dto.GetUserDto;
|
|
||||||
import de.szut.casino.user.dto.KeycloakUserDto;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class UserService {
|
|
||||||
@Autowired
|
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private RestTemplate http;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserMappingService mappingService;
|
|
||||||
|
|
||||||
public UserEntity createUser(CreateUserDto createUserDto) {
|
|
||||||
UserEntity user = mappingService.mapToUserEntity(createUserDto);
|
|
||||||
userRepository.save(user);
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GetUserDto getUser(String keycloakId) {
|
|
||||||
Optional<UserEntity> user = this.userRepository.findOneByKeycloakId(keycloakId);
|
|
||||||
|
|
||||||
return user.map(userEntity -> mappingService.mapToGetUserDto(userEntity)).orElse(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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package de.szut.casino.user.dto;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class CreateUserDto {
|
|
||||||
private String keycloakId;
|
|
||||||
private String username;
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package de.szut.casino.user.dto;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class GetUserDto {
|
|
||||||
private String keycloakId;
|
|
||||||
private String username;
|
|
||||||
private float balance;
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
@ -27,7 +27,7 @@ export const initializeKeycloak = (keycloak: KeycloakService) => async () =>
|
|||||||
onLoad: 'check-sso',
|
onLoad: 'check-sso',
|
||||||
silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html',
|
silentCheckSsoRedirectUri: window.location.origin + '/silent-check-sso.html',
|
||||||
checkLoginIframe: false,
|
checkLoginIframe: false,
|
||||||
redirectUri: window.location.origin + '/',
|
redirectUri: 'http://localhost:4200',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { Routes } from '@angular/router';
|
import { Routes } from '@angular/router';
|
||||||
import { LandingComponent } from './feature/landing/landing.component';
|
import { LandingComponent } from './feature/landing/landing.component';
|
||||||
|
import { HomeComponent } from './feature/home/home.component';
|
||||||
import { authGuard } from './auth.guard';
|
import { authGuard } from './auth.guard';
|
||||||
|
|
||||||
export const routes: Routes = [
|
export const routes: Routes = [
|
||||||
@ -7,13 +8,9 @@ export const routes: Routes = [
|
|||||||
path: '',
|
path: '',
|
||||||
component: LandingComponent,
|
component: LandingComponent,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
path: 'login/success',
|
|
||||||
loadComponent: () => import('./feature/login-success/login-success.component'),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
path: 'home',
|
path: 'home',
|
||||||
loadComponent: () => import('./feature/home/home.component'),
|
component: HomeComponent,
|
||||||
canActivate: [authGuard],
|
canActivate: [authGuard],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
@ -1,16 +1,23 @@
|
|||||||
import { CanActivateFn, Router } from '@angular/router';
|
import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from '@angular/router';
|
||||||
import { inject } from '@angular/core';
|
import { inject } from '@angular/core';
|
||||||
import { KeycloakService } from 'keycloak-angular';
|
import { KeycloakService } from 'keycloak-angular';
|
||||||
|
|
||||||
export const authGuard: CanActivateFn = async () => {
|
export const authGuard: CanActivateFn = async (
|
||||||
|
route: ActivatedRouteSnapshot,
|
||||||
|
state: RouterStateSnapshot
|
||||||
|
) => {
|
||||||
const keycloakService = inject(KeycloakService);
|
const keycloakService = inject(KeycloakService);
|
||||||
const router = inject(Router);
|
const isLoggedIn = keycloakService.isLoggedIn();
|
||||||
|
|
||||||
if (keycloakService.isLoggedIn()) {
|
if (isLoggedIn) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
router.navigate(['']);
|
const baseurl = window.location.origin;
|
||||||
|
|
||||||
|
keycloakService.login({
|
||||||
|
redirectUri: `${baseurl}${state.url}`,
|
||||||
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
@ -24,7 +24,7 @@ interface Transaction {
|
|||||||
templateUrl: './home.component.html',
|
templateUrl: './home.component.html',
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export default class HomeComponent {
|
export class HomeComponent {
|
||||||
private keycloakService: KeycloakService = inject(KeycloakService);
|
private keycloakService: KeycloakService = inject(KeycloakService);
|
||||||
public dialog: MatDialog = inject(MatDialog);
|
public dialog: MatDialog = inject(MatDialog);
|
||||||
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
<p>Logging in...</p>
|
|
@ -1,26 +0,0 @@
|
|||||||
import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
|
|
||||||
import { UserService } from '../../service/user.service';
|
|
||||||
import { KeycloakService } from 'keycloak-angular';
|
|
||||||
import { Router } from '@angular/router';
|
|
||||||
|
|
||||||
@Component({
|
|
||||||
selector: 'app-login-success',
|
|
||||||
standalone: true,
|
|
||||||
imports: [],
|
|
||||||
templateUrl: './login-success.component.html',
|
|
||||||
styleUrl: './login-success.component.css',
|
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
||||||
})
|
|
||||||
export default class LoginSuccessComponent implements OnInit {
|
|
||||||
private userService: UserService = inject(UserService);
|
|
||||||
private keycloakService: KeycloakService = inject(KeycloakService);
|
|
||||||
private router: Router = inject(Router);
|
|
||||||
|
|
||||||
async ngOnInit() {
|
|
||||||
const userProfile = await this.keycloakService.loadUserProfile();
|
|
||||||
const user = await this.userService.getOrCreateUser(userProfile);
|
|
||||||
sessionStorage.setItem('user', JSON.stringify(user));
|
|
||||||
|
|
||||||
this.router.navigate(['']);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
export interface User {
|
|
||||||
keycloakId: string;
|
|
||||||
username: string;
|
|
||||||
balance: number;
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
import { inject, Injectable } from '@angular/core';
|
|
||||||
import { HttpClient } from '@angular/common/http';
|
|
||||||
import { KeycloakProfile } from 'keycloak-js';
|
|
||||||
import { catchError, EMPTY, Observable } from 'rxjs';
|
|
||||||
import { User } from '../model/User';
|
|
||||||
|
|
||||||
@Injectable({
|
|
||||||
providedIn: 'root',
|
|
||||||
})
|
|
||||||
export class UserService {
|
|
||||||
private http: HttpClient = inject(HttpClient);
|
|
||||||
|
|
||||||
public getUser(id: string): Observable<User | null> {
|
|
||||||
return this.http.get<User | null>(`/backend/user/${id}`).pipe(catchError(() => EMPTY));
|
|
||||||
}
|
|
||||||
|
|
||||||
public createUser(id: string, username: string): Observable<User> {
|
|
||||||
return this.http.post<User>('/backend/user', {
|
|
||||||
keycloakId: id,
|
|
||||||
username: username,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getOrCreateUser(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();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -18,7 +18,7 @@ export class NavbarComponent {
|
|||||||
login() {
|
login() {
|
||||||
try {
|
try {
|
||||||
const baseUrl = window.location.origin;
|
const baseUrl = window.location.origin;
|
||||||
this.keycloakService.login({ redirectUri: `${baseUrl}/login/success` });
|
this.keycloakService.login({ redirectUri: `${baseUrl}/home` });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Login failed:', error);
|
console.error('Login failed:', error);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user