Merge branch 'main' into task/CAS-68/AdjustStartMoney
All checks were successful
CI / Get Changed Files (pull_request) Successful in 11s
CI / Docker frontend validation (pull_request) Successful in 16s
CI / oxlint (pull_request) Successful in 29s
CI / eslint (pull_request) Successful in 37s
CI / Checkstyle Main (pull_request) Successful in 1m19s
CI / Docker backend validation (pull_request) Successful in 1m27s
CI / prettier (pull_request) Successful in 23s
CI / test-build (pull_request) Successful in 43s
All checks were successful
CI / Get Changed Files (pull_request) Successful in 11s
CI / Docker frontend validation (pull_request) Successful in 16s
CI / oxlint (pull_request) Successful in 29s
CI / eslint (pull_request) Successful in 37s
CI / Checkstyle Main (pull_request) Successful in 1m19s
CI / Docker backend validation (pull_request) Successful in 1m27s
CI / prettier (pull_request) Successful in 23s
CI / test-build (pull_request) Successful in 43s
This commit is contained in:
commit
88b68f0547
9 changed files with 255 additions and 3 deletions
|
@ -8,6 +8,10 @@ import org.springframework.boot.CommandLineRunner;
|
|||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.mail.MailSender;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
@ -26,6 +30,11 @@ public class CasinoApplication {
|
|||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public static JavaMailSenderImpl javaMailSenderImpl() {
|
||||
return new JavaMailSenderImpl();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner initData(LootBoxRepository lootBoxRepository, RewardRepository rewardRepository) {
|
||||
return _ -> {
|
||||
|
|
|
@ -5,6 +5,7 @@ import de.szut.casino.security.dto.LoginRequestDto;
|
|||
import de.szut.casino.security.service.AuthService;
|
||||
import de.szut.casino.user.dto.CreateUserDto;
|
||||
import de.szut.casino.user.dto.GetUserDto;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
public class AuthController {
|
||||
|
@ -27,7 +30,7 @@ public class AuthController {
|
|||
}
|
||||
|
||||
@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);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import de.szut.casino.user.UserEntity;
|
|||
import de.szut.casino.user.UserService;
|
||||
import de.szut.casino.user.dto.CreateUserDto;
|
||||
import de.szut.casino.user.dto.GetUserDto;
|
||||
import jakarta.mail.MessagingException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
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.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Service
|
||||
public class AuthService {
|
||||
|
||||
|
@ -26,6 +29,9 @@ public class AuthService {
|
|||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private EmailService emailService;
|
||||
|
||||
public AuthResponseDto login(LoginRequestDto loginRequest) {
|
||||
Authentication authentication = authenticationManager.authenticate(
|
||||
new UsernamePasswordAuthenticationToken(
|
||||
|
@ -38,8 +44,11 @@ public class AuthService {
|
|||
return new AuthResponseDto(jwt);
|
||||
}
|
||||
|
||||
public GetUserDto register(CreateUserDto signUpRequest) {
|
||||
public GetUserDto register(CreateUserDto signUpRequest) throws MessagingException, IOException {
|
||||
UserEntity user = userService.createUser(signUpRequest);
|
||||
|
||||
this.emailService.sendRegistrationEmail(user);
|
||||
|
||||
return new GetUserDto(
|
||||
user.getId(),
|
||||
user.getEmail(),
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
package de.szut.casino.security.service;
|
||||
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Service
|
||||
public class EmailService {
|
||||
private JavaMailSenderImpl mailSender;
|
||||
private MailConfig mailConfig;
|
||||
@Value("${app.frontend-host}")
|
||||
private String feUrl;
|
||||
|
||||
public EmailService(JavaMailSenderImpl mailSender, MailConfig mailConfig) {
|
||||
this.mailSender = mailSender;
|
||||
this.mailConfig = mailConfig;
|
||||
this.mailSender.setHost(mailConfig.host);
|
||||
this.mailSender.setPort(mailConfig.port);
|
||||
if (mailConfig.authenticationEnabled) {
|
||||
this.mailSender.setUsername(mailConfig.username);
|
||||
this.mailSender.setPassword(mailConfig.password);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendRegistrationEmail(UserEntity user) throws IOException, MessagingException {
|
||||
String template = loadTemplate("email/welcome.html");
|
||||
String htmlContent = template
|
||||
.replace("${username}", user.getUsername())
|
||||
.replace("${feUrl}", feUrl);
|
||||
|
||||
MimeMessage message = mailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||
|
||||
helper.setFrom(mailConfig.fromAddress);
|
||||
helper.setTo(user.getEmail());
|
||||
helper.setSubject("Willkommen bei Trustworthy Casino©");
|
||||
helper.setText(htmlContent, true);
|
||||
|
||||
mailSender.send(message);
|
||||
}
|
||||
|
||||
private String loadTemplate(String templatePath) throws IOException {
|
||||
ClassPathResource resource = new ClassPathResource("templates/" + templatePath);
|
||||
try (Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
|
||||
return FileCopyUtils.copyToString(reader);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package de.szut.casino.security.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MailConfig {
|
||||
@Value("${app.mail.host}")
|
||||
public String host;
|
||||
|
||||
@Value("${app.mail.port}")
|
||||
public Integer port;
|
||||
|
||||
@Value("${app.mail.authentication}")
|
||||
public Boolean authenticationEnabled;
|
||||
|
||||
@Value("${app.mail.username}")
|
||||
public String username;
|
||||
|
||||
@Value("${app.mail.password}")
|
||||
public String password;
|
||||
|
||||
@Value("${app.mail.from-address}")
|
||||
public String fromAddress;
|
||||
}
|
Reference in a new issue