feat(auth): add email verification functionality
This commit is contained in:
parent
954e1ea6ea
commit
59aa831981
7 changed files with 179 additions and 5 deletions
|
@ -47,7 +47,7 @@ public class AuthService {
|
||||||
public GetUserDto register(CreateUserDto signUpRequest) throws MessagingException, IOException {
|
public GetUserDto register(CreateUserDto signUpRequest) throws MessagingException, IOException {
|
||||||
UserEntity user = userService.createUser(signUpRequest);
|
UserEntity user = userService.createUser(signUpRequest);
|
||||||
|
|
||||||
this.emailService.sendRegistrationEmail(user);
|
this.emailService.sendEmailVerificationEmail(user);
|
||||||
|
|
||||||
return new GetUserDto(
|
return new GetUserDto(
|
||||||
user.getId(),
|
user.getId(),
|
||||||
|
|
|
@ -35,6 +35,24 @@ public class EmailService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sendEmailVerificationEmail(UserEntity user) throws IOException, MessagingException {
|
||||||
|
String template = loadTemplate("email/verify.html");
|
||||||
|
String htmlContent = template
|
||||||
|
.replace("${username}", user.getUsername())
|
||||||
|
.replace("${feUrl}", feUrl)
|
||||||
|
.replace("${token}", user.getVerificationToken());
|
||||||
|
|
||||||
|
MimeMessage message = mailSender.createMimeMessage();
|
||||||
|
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
|
||||||
|
|
||||||
|
helper.setFrom(mailConfig.fromAddress);
|
||||||
|
helper.setTo(user.getEmailAddress());
|
||||||
|
helper.setSubject("E-Mail Bestätigung");
|
||||||
|
helper.setText(htmlContent, true);
|
||||||
|
|
||||||
|
mailSender.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
public void sendRegistrationEmail(UserEntity user) throws IOException, MessagingException {
|
public void sendRegistrationEmail(UserEntity user) throws IOException, MessagingException {
|
||||||
String template = loadTemplate("email/welcome.html");
|
String template = loadTemplate("email/welcome.html");
|
||||||
String htmlContent = template
|
String htmlContent = template
|
||||||
|
|
|
@ -30,11 +30,16 @@ public class UserEntity {
|
||||||
@Column(precision = 19, scale = 2)
|
@Column(precision = 19, scale = 2)
|
||||||
private BigDecimal balance;
|
private BigDecimal balance;
|
||||||
|
|
||||||
public UserEntity(String email, String username, String password, BigDecimal balance) {
|
private Boolean emailVerified = false;
|
||||||
|
|
||||||
|
private String verificationToken;
|
||||||
|
|
||||||
|
public UserEntity(String email, String username, String password, BigDecimal balance, String verificationToken) {
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.balance = balance;
|
this.balance = balance;
|
||||||
|
this.verificationToken = verificationToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addBalance(BigDecimal amountToAdd) {
|
public void addBalance(BigDecimal amountToAdd) {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package de.szut.casino.user;
|
||||||
|
|
||||||
import de.szut.casino.user.dto.CreateUserDto;
|
import de.szut.casino.user.dto.CreateUserDto;
|
||||||
import jakarta.persistence.EntityExistsException;
|
import jakarta.persistence.EntityExistsException;
|
||||||
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
@ -31,7 +32,8 @@ public class UserService {
|
||||||
createUserDto.getEmail(),
|
createUserDto.getEmail(),
|
||||||
createUserDto.getUsername(),
|
createUserDto.getUsername(),
|
||||||
passwordEncoder.encode(createUserDto.getPassword()),
|
passwordEncoder.encode(createUserDto.getPassword()),
|
||||||
BigDecimal.valueOf(100) // Starting balance
|
BigDecimal.valueOf(100),
|
||||||
|
RandomStringUtils.randomAlphanumeric(64)
|
||||||
);
|
);
|
||||||
|
|
||||||
return userRepository.save(user);
|
return userRepository.save(user);
|
||||||
|
|
|
@ -128,7 +128,7 @@
|
||||||
Ihr <span style="color: #10b981;">Trustworthy Casino</span> Team</p>
|
Ihr <span style="color: #10b981;">Trustworthy Casino</span> Team</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="footer">
|
<div class="footer">
|
||||||
<p>2025 Trustworthy Casino - Alle Rechte vorbehalten</p>
|
<p>2025 Trustworthy Casino - Keine Rechte vorbehalten</p>
|
||||||
<p>Diese E-Mail wurde automatisch generiert. Bitte antworten Sie nicht darauf.</p>
|
<p>Diese E-Mail wurde automatisch generiert. Bitte antworten Sie nicht darauf.</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
149
backend/src/main/resources/templates/email/verify.html
Normal file
149
backend/src/main/resources/templates/email/verify.html
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>E-Mail-Verifizierung - Trustworthy Casino©</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: 'Arial', sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
background-color: #f8fafc;
|
||||||
|
color: #64748b;
|
||||||
|
max-width: 600px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
background-color: #0a1219;
|
||||||
|
border-radius: 8px;
|
||||||
|
overflow: hidden;
|
||||||
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
.header {
|
||||||
|
background-color: #1a2835;
|
||||||
|
padding: 20px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.header h1 {
|
||||||
|
color: #ffffff;
|
||||||
|
margin: 0;
|
||||||
|
font-size: 28px;
|
||||||
|
}
|
||||||
|
.content {
|
||||||
|
background-color: #121e27;
|
||||||
|
padding: 30px;
|
||||||
|
color: #ffffff;
|
||||||
|
}
|
||||||
|
.footer {
|
||||||
|
background-color: #1a2835;
|
||||||
|
color: #94a3b8;
|
||||||
|
padding: 20px;
|
||||||
|
text-align: center;
|
||||||
|
font-size: 0.8em;
|
||||||
|
}
|
||||||
|
.button {
|
||||||
|
display: inline-block;
|
||||||
|
background-color: #10b981;
|
||||||
|
color: #ffffff;
|
||||||
|
padding: 12px 24px;
|
||||||
|
margin: 20px 0;
|
||||||
|
text-decoration: none;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-weight: bold;
|
||||||
|
text-align: center;
|
||||||
|
transition: background-color 0.3s;
|
||||||
|
}
|
||||||
|
.button:hover {
|
||||||
|
background-color: #059669;
|
||||||
|
}
|
||||||
|
h2 {
|
||||||
|
color: #ffffff;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
ul {
|
||||||
|
padding-left: 20px;
|
||||||
|
margin: 20px 0;
|
||||||
|
}
|
||||||
|
li {
|
||||||
|
margin-bottom: 8px;
|
||||||
|
color: #94a3b8;
|
||||||
|
}
|
||||||
|
li::marker {
|
||||||
|
color: #34d399;
|
||||||
|
}
|
||||||
|
.highlight {
|
||||||
|
color: #10b981;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
.divider {
|
||||||
|
height: 1px;
|
||||||
|
background-color: #1a2835;
|
||||||
|
margin: 20px 0;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
margin: 16px 0;
|
||||||
|
}
|
||||||
|
.verification-code {
|
||||||
|
background-color: #1a2835;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 15px;
|
||||||
|
text-align: center;
|
||||||
|
margin: 20px 0;
|
||||||
|
letter-spacing: 5px;
|
||||||
|
font-size: 24px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #10b981;
|
||||||
|
}
|
||||||
|
.info-box {
|
||||||
|
background-color: #1a2835;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 15px;
|
||||||
|
margin: 20px 0;
|
||||||
|
}
|
||||||
|
.warning {
|
||||||
|
color: #f59e0b;
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="header">
|
||||||
|
<h1>Trustworthy Casino</h1>
|
||||||
|
</div>
|
||||||
|
<div class="content">
|
||||||
|
<h2>Hallo <span class="highlight">${username}</span>,</h2>
|
||||||
|
|
||||||
|
<p>vielen Dank für Ihre Registrierung bei Trustworthy Casino. Um Ihr Konto zu aktivieren und Zugang zu allen Funktionen zu erhalten, bestätigen Sie bitte Ihre E-Mail-Adresse.</p>
|
||||||
|
|
||||||
|
<div class="divider"></div>
|
||||||
|
|
||||||
|
<p>Klicken Sie auf den folgenden Button, um Ihre E-Mail-Adresse zu bestätigen:</p>
|
||||||
|
|
||||||
|
<div style="text-align: center;">
|
||||||
|
<a href="${feUrl}/backend/verify?token=${token}" class="button">E-Mail bestätigen</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="info-box">
|
||||||
|
<p><span class="warning">Hinweis:</span> Der Bestätigungscode könnte nur 24 Stunden gültig sein und kann vielleicht auch nur einmal verwendet werden.</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="divider"></div>
|
||||||
|
|
||||||
|
<p>Nach der Bestätigung Ihrer E-Mail-Adresse können Sie sofort mit dem Spielen beginnen und alle Vorteile Ihres Kontos nutzen.</p>
|
||||||
|
|
||||||
|
<p>Bei Fragen stehen wir Ihnen jederzeit zur Verfügung.</p>
|
||||||
|
|
||||||
|
<p>Mit freundlichen Grüßen,<br>
|
||||||
|
Ihr <span style="color: #10b981;">Trustworthy Casino</span> Team</p>
|
||||||
|
</div>
|
||||||
|
<div class="footer">
|
||||||
|
<p>2025 Trustworthy Casino - Keine Rechte vorbehalten</p>
|
||||||
|
<p>Diese E-Mail wurde automatisch generiert. Bitte antworten Sie nicht darauf.</p>
|
||||||
|
<p>Falls Sie diese E-Mail nicht angefordert haben, ignorieren Sie diese bitte.</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -120,7 +120,7 @@
|
||||||
Ihr <span style="color: #10b981;">Trustworthy Casino</span> Team</p>
|
Ihr <span style="color: #10b981;">Trustworthy Casino</span> Team</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="footer">
|
<div class="footer">
|
||||||
<p>2025 Trustworthy Casino - Alle Rechte vorbehalten</p>
|
<p>2025 Trustworthy Casino - Keine Rechte vorbehalten</p>
|
||||||
<p>Diese E-Mail wurde automatisch generiert. Bitte antworten Sie nicht darauf.</p>
|
<p>Diese E-Mail wurde automatisch generiert. Bitte antworten Sie nicht darauf.</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Reference in a new issue