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 {
|
||||
UserEntity user = userService.createUser(signUpRequest);
|
||||
|
||||
this.emailService.sendRegistrationEmail(user);
|
||||
this.emailService.sendEmailVerificationEmail(user);
|
||||
|
||||
return new GetUserDto(
|
||||
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 {
|
||||
String template = loadTemplate("email/welcome.html");
|
||||
String htmlContent = template
|
||||
|
|
|
@ -30,11 +30,16 @@ public class UserEntity {
|
|||
@Column(precision = 19, scale = 2)
|
||||
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.username = username;
|
||||
this.password = password;
|
||||
this.balance = balance;
|
||||
this.verificationToken = verificationToken;
|
||||
}
|
||||
|
||||
public void addBalance(BigDecimal amountToAdd) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package de.szut.casino.user;
|
|||
|
||||
import de.szut.casino.user.dto.CreateUserDto;
|
||||
import jakarta.persistence.EntityExistsException;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
@ -31,7 +32,8 @@ public class UserService {
|
|||
createUserDto.getEmail(),
|
||||
createUserDto.getUsername(),
|
||||
passwordEncoder.encode(createUserDto.getPassword()),
|
||||
BigDecimal.valueOf(100) // Starting balance
|
||||
BigDecimal.valueOf(100),
|
||||
RandomStringUtils.randomAlphanumeric(64)
|
||||
);
|
||||
|
||||
return userRepository.save(user);
|
||||
|
|
Reference in a new issue