[dfd5d87] | 1 | package finki.it.phoneluxbackend.services;
|
---|
| 2 |
|
---|
| 3 | import finki.it.phoneluxbackend.data.RegistrationRequest;
|
---|
| 4 | import finki.it.phoneluxbackend.data.UserRole;
|
---|
| 5 | import finki.it.phoneluxbackend.entities.ConfirmationToken;
|
---|
| 6 | import finki.it.phoneluxbackend.entities.User;
|
---|
| 7 | import finki.it.phoneluxbackend.security.email.EmailSender;
|
---|
| 8 | import finki.it.phoneluxbackend.security.email.EmailValidator;
|
---|
| 9 | import lombok.AllArgsConstructor;
|
---|
| 10 | import org.springframework.stereotype.Service;
|
---|
| 11 | import org.springframework.transaction.annotation.Transactional;
|
---|
| 12 |
|
---|
| 13 | import java.time.LocalDateTime;
|
---|
| 14 |
|
---|
| 15 | @Service
|
---|
| 16 | @AllArgsConstructor
|
---|
| 17 | public class RegistrationService {
|
---|
| 18 | private final UserService userService;
|
---|
| 19 | private final EmailValidator emailValidator;
|
---|
| 20 | private final ConfirmationTokenService confirmationTokenService;
|
---|
| 21 | private final EmailSender emailSender;
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | public String register(RegistrationRequest request) {
|
---|
| 25 | boolean isValidEmail = emailValidator.test(request.getEmail());
|
---|
| 26 |
|
---|
| 27 | // validacija za mejl na frontend ?
|
---|
| 28 | if (!isValidEmail)
|
---|
| 29 | throw new IllegalStateException("Email"+request.getEmail()+" not valid!");
|
---|
| 30 |
|
---|
| 31 | String token = userService.signUpUser(
|
---|
| 32 | new User(request.getFirstName(),
|
---|
| 33 | request.getLastName(),
|
---|
| 34 | request.getEmail(),
|
---|
| 35 | request.getPassword(),
|
---|
| 36 | UserRole.USER));
|
---|
| 37 |
|
---|
| 38 | String link = "http://localhost:8080/registration/confirm?token="+token;
|
---|
| 39 | emailSender.send(request.getEmail(), buildEmail(request.getFirstName(),link));
|
---|
| 40 | return token;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | private String buildEmail(String name, String link) {
|
---|
| 44 | return "<div style=\"font-family:Helvetica,Arial,sans-serif;font-size:16px;margin:0;color:#0b0c0c\">\n" +
|
---|
| 45 | "\n" +
|
---|
| 46 | "<span style=\"display:none;font-size:1px;color:#fff;max-height:0\"></span>\n" +
|
---|
| 47 | "\n" +
|
---|
| 48 | " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;min-width:100%;width:100%!important\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" +
|
---|
| 49 | " <tbody><tr>\n" +
|
---|
| 50 | " <td width=\"100%\" height=\"53\" bgcolor=\"#0b0c0c\">\n" +
|
---|
| 51 | " \n" +
|
---|
| 52 | " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;max-width:580px\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\">\n" +
|
---|
| 53 | " <tbody><tr>\n" +
|
---|
| 54 | " <td width=\"70\" bgcolor=\"#0b0c0c\" valign=\"middle\">\n" +
|
---|
| 55 | " <table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +
|
---|
| 56 | " <tbody><tr>\n" +
|
---|
| 57 | " <td style=\"padding-left:10px\">\n" +
|
---|
| 58 | " \n" +
|
---|
| 59 | " </td>\n" +
|
---|
| 60 | " <td style=\"font-size:28px;line-height:1.315789474;Margin-top:4px;padding-left:10px\">\n" +
|
---|
| 61 | " <span style=\"font-family:Helvetica,Arial,sans-serif;font-weight:700;color:#ffffff;text-decoration:none;vertical-align:top;display:inline-block\">Confirm your email</span>\n" +
|
---|
| 62 | " </td>\n" +
|
---|
| 63 | " </tr>\n" +
|
---|
| 64 | " </tbody></table>\n" +
|
---|
| 65 | " </a>\n" +
|
---|
| 66 | " </td>\n" +
|
---|
| 67 | " </tr>\n" +
|
---|
| 68 | " </tbody></table>\n" +
|
---|
| 69 | " \n" +
|
---|
| 70 | " </td>\n" +
|
---|
| 71 | " </tr>\n" +
|
---|
| 72 | " </tbody></table>\n" +
|
---|
| 73 | " <table role=\"presentation\" class=\"m_-6186904992287805515content\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;max-width:580px;width:100%!important\" width=\"100%\">\n" +
|
---|
| 74 | " <tbody><tr>\n" +
|
---|
| 75 | " <td width=\"10\" height=\"10\" valign=\"middle\"></td>\n" +
|
---|
| 76 | " <td>\n" +
|
---|
| 77 | " \n" +
|
---|
| 78 | " <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +
|
---|
| 79 | " <tbody><tr>\n" +
|
---|
| 80 | " <td bgcolor=\"#1D70B8\" width=\"100%\" height=\"10\"></td>\n" +
|
---|
| 81 | " </tr>\n" +
|
---|
| 82 | " </tbody></table>\n" +
|
---|
| 83 | " \n" +
|
---|
| 84 | " </td>\n" +
|
---|
| 85 | " <td width=\"10\" valign=\"middle\" height=\"10\"></td>\n" +
|
---|
| 86 | " </tr>\n" +
|
---|
| 87 | " </tbody></table>\n" +
|
---|
| 88 | "\n" +
|
---|
| 89 | "\n" +
|
---|
| 90 | "\n" +
|
---|
| 91 | " <table role=\"presentation\" class=\"m_-6186904992287805515content\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse;max-width:580px;width:100%!important\" width=\"100%\">\n" +
|
---|
| 92 | " <tbody><tr>\n" +
|
---|
| 93 | " <td height=\"30\"><br></td>\n" +
|
---|
| 94 | " </tr>\n" +
|
---|
| 95 | " <tr>\n" +
|
---|
| 96 | " <td width=\"10\" valign=\"middle\"><br></td>\n" +
|
---|
| 97 | " <td style=\"font-family:Helvetica,Arial,sans-serif;font-size:19px;line-height:1.315789474;max-width:560px\">\n" +
|
---|
| 98 | " \n" +
|
---|
| 99 | " <p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\">Hi " + name + ",</p><p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\"> Thank you for registering. Please click on the below link to activate your account: </p><blockquote style=\"Margin:0 0 20px 0;border-left:10px solid #b1b4b6;padding:15px 0 0.1px 15px;font-size:19px;line-height:25px\"><p style=\"Margin:0 0 20px 0;font-size:19px;line-height:25px;color:#0b0c0c\"> <a href=\"" + link + "\">Activate Now</a> </p></blockquote>\n Link will expire in 15 minutes. <p>See you soon</p>" +
|
---|
| 100 | " \n" +
|
---|
| 101 | " </td>\n" +
|
---|
| 102 | " <td width=\"10\" valign=\"middle\"><br></td>\n" +
|
---|
| 103 | " </tr>\n" +
|
---|
| 104 | " <tr>\n" +
|
---|
| 105 | " <td height=\"30\"><br></td>\n" +
|
---|
| 106 | " </tr>\n" +
|
---|
| 107 | " </tbody></table><div class=\"yj6qo\"></div><div class=\"adL\">\n" +
|
---|
| 108 | "\n" +
|
---|
| 109 | "</div></div>";
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | @Transactional
|
---|
| 114 | public String confirmToken(String token) {
|
---|
| 115 | ConfirmationToken confirmationToken = confirmationTokenService.getToken(token)
|
---|
| 116 | .orElseThrow(() -> new IllegalStateException("Token not found!"));
|
---|
| 117 |
|
---|
| 118 | if(confirmationToken.getConfirmedAt() != null)
|
---|
| 119 | throw new IllegalStateException("Email already confirmed!");
|
---|
| 120 |
|
---|
| 121 | LocalDateTime expiresAt = confirmationToken.getExpiresAt();
|
---|
| 122 |
|
---|
| 123 | if (expiresAt.isBefore(LocalDateTime.now())){
|
---|
| 124 | throw new IllegalStateException("Token expired");
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | confirmationTokenService.setConfirmedAt(token);
|
---|
| 128 | userService.enableUser(confirmationToken.getUser().getEmail());
|
---|
| 129 |
|
---|
| 130 | return "Email confirmed";
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | }
|
---|