feat: whoops
All checks were successful
CI / Get Changed Files (pull_request) Successful in 7s
CI / eslint (pull_request) Has been skipped
CI / oxlint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Docker frontend validation (pull_request) Successful in 45s
CI / Docker backend validation (pull_request) Successful in 1m2s
CI / Checkstyle Main (pull_request) Successful in 1m1s

This commit is contained in:
csimonis 2025-05-14 08:44:37 +02:00 committed by Constantin Simonis
commit 73710a1542
4 changed files with 36 additions and 44 deletions

View file

@ -1,8 +1,5 @@
package de.szut.casino.health; package de.szut.casino.health;
import de.szut.casino.security.service.EmailService;
import de.szut.casino.user.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -11,17 +8,8 @@ import java.util.Map;
@RestController @RestController
public class HealthController { public class HealthController {
@Autowired
private EmailService emailService;
@GetMapping("/health") @GetMapping("/health")
public Map<String, String> healthCheck() { public Map<String, String> healthCheck() {
UserEntity user = new UserEntity();
user.setEmail("user@mail.com");
user.setUsername("user");
this.emailService.sendRegistrationEmail(user);
return Map.of("status", "UP"); return Map.of("status", "UP");
} }
} }

View file

@ -5,6 +5,7 @@ import de.szut.casino.security.dto.LoginRequestDto;
import de.szut.casino.security.service.AuthService; import de.szut.casino.security.service.AuthService;
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 jakarta.mail.MessagingException;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -13,6 +14,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController @RestController
@RequestMapping("/auth") @RequestMapping("/auth")
public class AuthController { public class AuthController {
@ -27,7 +30,7 @@ public class AuthController {
} }
@PostMapping("/register") @PostMapping("/register")
public ResponseEntity<GetUserDto> registerUser(@Valid @RequestBody CreateUserDto signUpRequest) { public ResponseEntity<GetUserDto> registerUser(@Valid @RequestBody CreateUserDto signUpRequest) throws MessagingException, IOException {
GetUserDto response = authService.register(signUpRequest); GetUserDto response = authService.register(signUpRequest);
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} }

View file

@ -7,6 +7,7 @@ import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserService; import de.szut.casino.user.UserService;
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 jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -14,6 +15,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
@Service @Service
public class AuthService { public class AuthService {
@ -26,6 +29,9 @@ public class AuthService {
@Autowired @Autowired
private UserService userService; private UserService userService;
@Autowired
private EmailService emailService;
public AuthResponseDto login(LoginRequestDto loginRequest) { public AuthResponseDto login(LoginRequestDto loginRequest) {
Authentication authentication = authenticationManager.authenticate( Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken( new UsernamePasswordAuthenticationToken(
@ -38,8 +44,11 @@ public class AuthService {
return new AuthResponseDto(jwt); return new AuthResponseDto(jwt);
} }
public GetUserDto register(CreateUserDto signUpRequest) { public GetUserDto register(CreateUserDto signUpRequest) throws MessagingException, IOException {
UserEntity user = userService.createUser(signUpRequest); UserEntity user = userService.createUser(signUpRequest);
this.emailService.sendRegistrationEmail(user);
return new GetUserDto( return new GetUserDto(
user.getId(), user.getId(),
user.getEmail(), user.getEmail(),

View file

@ -23,41 +23,33 @@ public class EmailService {
private String feUrl; private String feUrl;
public EmailService(JavaMailSenderImpl mailSender, MailConfig mailConfig) { public EmailService(JavaMailSenderImpl mailSender, MailConfig mailConfig) {
try { this.mailSender = mailSender;
this.mailSender = mailSender; this.mailConfig = mailConfig;
this.mailConfig = mailConfig; this.mailSender.setHost(mailConfig.host);
this.mailSender.setHost(mailConfig.host); this.mailSender.setPort(mailConfig.port);
this.mailSender.setPort(mailConfig.port); if (mailConfig.authenticationEnabled) {
if (mailConfig.authenticationEnabled) { this.mailSender.setUsername(mailConfig.username);
this.mailSender.setUsername(mailConfig.username); this.mailSender.setPassword(mailConfig.password);
this.mailSender.setPassword(mailConfig.password);
}
} catch (Exception e) {
System.out.println(e.getMessage());
} }
} }
public void sendRegistrationEmail(UserEntity user) { public void sendRegistrationEmail(UserEntity user) throws IOException, MessagingException {
try { String template = loadTemplate("email/welcome.html");
String template = loadTemplate("email/welcome.html"); String htmlContent = template
String htmlContent = template .replace("${username}", user.getUsername())
.replace("${username}", user.getUsername()) .replace("${feUrl}", feUrl);
.replace("${feUrl}", feUrl);
MimeMessage message = mailSender.createMimeMessage(); MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
helper.setFrom(mailConfig.fromAddress); helper.setFrom(mailConfig.fromAddress);
helper.setTo(user.getEmail()); helper.setTo(user.getEmail());
helper.setSubject("Willkommen bei Trustworthy Casino©"); helper.setSubject("Willkommen bei Trustworthy Casino©");
helper.setText(htmlContent, true); helper.setText(htmlContent, true);
mailSender.send(message); mailSender.send(message);
} catch (MessagingException | IOException e) {
System.err.println("Failed to send registration email: " + e.getMessage());
}
} }
private String loadTemplate(String templatePath) throws IOException { private String loadTemplate(String templatePath) throws IOException {
ClassPathResource resource = new ClassPathResource("templates/" + templatePath); ClassPathResource resource = new ClassPathResource("templates/" + templatePath);
try (Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { try (Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {