package finki.it.phoneluxbackend.services; import finki.it.phoneluxbackend.data.RegistrationRequest; import finki.it.phoneluxbackend.data.UserRole; import finki.it.phoneluxbackend.entities.ConfirmationToken; import finki.it.phoneluxbackend.entities.User; import finki.it.phoneluxbackend.security.email.EmailSender; import finki.it.phoneluxbackend.security.email.EmailValidator; import lombok.AllArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; @Service @AllArgsConstructor public class RegistrationService { private final UserService userService; private final EmailValidator emailValidator; private final ConfirmationTokenService confirmationTokenService; private final EmailSender emailSender; public ResponseEntity register(RegistrationRequest request) { boolean isValidEmail = emailValidator.test(request.getEmail()); // mail is validated on frontend already if (!isValidEmail) throw new IllegalStateException("Email"+request.getEmail()+" not valid!"); ResponseEntity response = userService.signUpUser( new User(request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), UserRole.USER)); if (response.getStatusCode() == HttpStatus.BAD_REQUEST) { return response; } String link = "http://localhost:8080/registration/confirm?token="+response.getBody() .toString().split(":")[1]; emailSender.send(request.getEmail(), buildEmail(request.getFirstName(),link)); return response; } private String buildEmail(String name, String link) { return "
\n" + "\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + " \n" + " Confirm your email\n" + "
\n" + " \n" + "
\n" + " \n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + "
\n" + "\n" + "\n" + "\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "


\n" + " \n" + "

Hi " + name + ",

Thank you for registering. Please click on the below link to activate your account:

Activate Now

\n Link will expire in 15 minutes.

See you soon

" + " \n" + "


\n" + "\n" + "
"; } @Transactional public String confirmToken(String token) { boolean confirmationTokenExists = confirmationTokenService.getToken(token).isPresent(); ConfirmationToken confirmationToken; if(confirmationTokenExists) confirmationToken = confirmationTokenService.getToken(token).get(); else return "Token not found!"; if(confirmationToken.getConfirmedAt() != null) return "Email already confirmed!"; LocalDateTime expiresAt = confirmationToken.getExpiresAt(); if (expiresAt.isBefore(LocalDateTime.now())){ return "Token expired"; } confirmationTokenService.setConfirmedAt(token); userService.enableUser(confirmationToken.getUser().getEmail()); return "Email confirmed"; } }