[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;
|
---|
[f25d07e] | 10 | import org.springframework.http.HttpStatus;
|
---|
| 11 | import org.springframework.http.ResponseEntity;
|
---|
[dfd5d87] | 12 | import org.springframework.stereotype.Service;
|
---|
| 13 | import org.springframework.transaction.annotation.Transactional;
|
---|
| 14 |
|
---|
| 15 | import java.time.LocalDateTime;
|
---|
| 16 |
|
---|
| 17 | @Service
|
---|
| 18 | @AllArgsConstructor
|
---|
| 19 | public class RegistrationService {
|
---|
| 20 | private final UserService userService;
|
---|
| 21 | private final EmailValidator emailValidator;
|
---|
| 22 | private final ConfirmationTokenService confirmationTokenService;
|
---|
| 23 | private final EmailSender emailSender;
|
---|
| 24 |
|
---|
| 25 |
|
---|
[f25d07e] | 26 | public ResponseEntity<Object> register(RegistrationRequest request) {
|
---|
[dfd5d87] | 27 | boolean isValidEmail = emailValidator.test(request.getEmail());
|
---|
| 28 |
|
---|
[f25d07e] | 29 | // mail is validated on frontend already
|
---|
[dfd5d87] | 30 | if (!isValidEmail)
|
---|
| 31 | throw new IllegalStateException("Email"+request.getEmail()+" not valid!");
|
---|
| 32 |
|
---|
[f25d07e] | 33 | ResponseEntity response = userService.signUpUser(
|
---|
[dfd5d87] | 34 | new User(request.getFirstName(),
|
---|
| 35 | request.getLastName(),
|
---|
| 36 | request.getEmail(),
|
---|
| 37 | request.getPassword(),
|
---|
| 38 | UserRole.USER));
|
---|
| 39 |
|
---|
[f25d07e] | 40 | if (response.getStatusCode() == HttpStatus.BAD_REQUEST)
|
---|
| 41 | {
|
---|
| 42 | return response;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | String link = "http://localhost:8080/registration/confirm?token="+response.getBody()
|
---|
| 46 | .toString().split(":")[1];
|
---|
[dfd5d87] | 47 | emailSender.send(request.getEmail(), buildEmail(request.getFirstName(),link));
|
---|
[f25d07e] | 48 |
|
---|
| 49 | return response;
|
---|
[dfd5d87] | 50 | }
|
---|
| 51 |
|
---|
| 52 | private String buildEmail(String name, String link) {
|
---|
| 53 | return "<div style=\"font-family:Helvetica,Arial,sans-serif;font-size:16px;margin:0;color:#0b0c0c\">\n" +
|
---|
| 54 | "\n" +
|
---|
| 55 | "<span style=\"display:none;font-size:1px;color:#fff;max-height:0\"></span>\n" +
|
---|
| 56 | "\n" +
|
---|
| 57 | " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;min-width:100%;width:100%!important\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n" +
|
---|
| 58 | " <tbody><tr>\n" +
|
---|
| 59 | " <td width=\"100%\" height=\"53\" bgcolor=\"#0b0c0c\">\n" +
|
---|
| 60 | " \n" +
|
---|
| 61 | " <table role=\"presentation\" width=\"100%\" style=\"border-collapse:collapse;max-width:580px\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" align=\"center\">\n" +
|
---|
| 62 | " <tbody><tr>\n" +
|
---|
| 63 | " <td width=\"70\" bgcolor=\"#0b0c0c\" valign=\"middle\">\n" +
|
---|
| 64 | " <table role=\"presentation\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +
|
---|
| 65 | " <tbody><tr>\n" +
|
---|
| 66 | " <td style=\"padding-left:10px\">\n" +
|
---|
| 67 | " \n" +
|
---|
| 68 | " </td>\n" +
|
---|
| 69 | " <td style=\"font-size:28px;line-height:1.315789474;Margin-top:4px;padding-left:10px\">\n" +
|
---|
| 70 | " <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" +
|
---|
| 71 | " </td>\n" +
|
---|
| 72 | " </tr>\n" +
|
---|
| 73 | " </tbody></table>\n" +
|
---|
| 74 | " </a>\n" +
|
---|
| 75 | " </td>\n" +
|
---|
| 76 | " </tr>\n" +
|
---|
| 77 | " </tbody></table>\n" +
|
---|
| 78 | " \n" +
|
---|
| 79 | " </td>\n" +
|
---|
| 80 | " </tr>\n" +
|
---|
| 81 | " </tbody></table>\n" +
|
---|
| 82 | " <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" +
|
---|
| 83 | " <tbody><tr>\n" +
|
---|
| 84 | " <td width=\"10\" height=\"10\" valign=\"middle\"></td>\n" +
|
---|
| 85 | " <td>\n" +
|
---|
| 86 | " \n" +
|
---|
| 87 | " <table role=\"presentation\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"border-collapse:collapse\">\n" +
|
---|
| 88 | " <tbody><tr>\n" +
|
---|
| 89 | " <td bgcolor=\"#1D70B8\" width=\"100%\" height=\"10\"></td>\n" +
|
---|
| 90 | " </tr>\n" +
|
---|
| 91 | " </tbody></table>\n" +
|
---|
| 92 | " \n" +
|
---|
| 93 | " </td>\n" +
|
---|
| 94 | " <td width=\"10\" valign=\"middle\" height=\"10\"></td>\n" +
|
---|
| 95 | " </tr>\n" +
|
---|
| 96 | " </tbody></table>\n" +
|
---|
| 97 | "\n" +
|
---|
| 98 | "\n" +
|
---|
| 99 | "\n" +
|
---|
| 100 | " <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" +
|
---|
| 101 | " <tbody><tr>\n" +
|
---|
| 102 | " <td height=\"30\"><br></td>\n" +
|
---|
| 103 | " </tr>\n" +
|
---|
| 104 | " <tr>\n" +
|
---|
| 105 | " <td width=\"10\" valign=\"middle\"><br></td>\n" +
|
---|
| 106 | " <td style=\"font-family:Helvetica,Arial,sans-serif;font-size:19px;line-height:1.315789474;max-width:560px\">\n" +
|
---|
| 107 | " \n" +
|
---|
| 108 | " <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>" +
|
---|
| 109 | " \n" +
|
---|
| 110 | " </td>\n" +
|
---|
| 111 | " <td width=\"10\" valign=\"middle\"><br></td>\n" +
|
---|
| 112 | " </tr>\n" +
|
---|
| 113 | " <tr>\n" +
|
---|
| 114 | " <td height=\"30\"><br></td>\n" +
|
---|
| 115 | " </tr>\n" +
|
---|
| 116 | " </tbody></table><div class=\"yj6qo\"></div><div class=\"adL\">\n" +
|
---|
| 117 | "\n" +
|
---|
| 118 | "</div></div>";
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 | @Transactional
|
---|
| 123 | public String confirmToken(String token) {
|
---|
[f25d07e] | 124 | boolean confirmationTokenExists = confirmationTokenService.getToken(token).isPresent();
|
---|
| 125 |
|
---|
| 126 | ConfirmationToken confirmationToken;
|
---|
| 127 |
|
---|
| 128 | if(confirmationTokenExists)
|
---|
| 129 | confirmationToken = confirmationTokenService.getToken(token).get();
|
---|
| 130 | else
|
---|
| 131 | return "Token not found!";
|
---|
[dfd5d87] | 132 |
|
---|
| 133 | if(confirmationToken.getConfirmedAt() != null)
|
---|
[f25d07e] | 134 | return "Email already confirmed!";
|
---|
[dfd5d87] | 135 |
|
---|
| 136 | LocalDateTime expiresAt = confirmationToken.getExpiresAt();
|
---|
| 137 |
|
---|
| 138 | if (expiresAt.isBefore(LocalDateTime.now())){
|
---|
[f25d07e] | 139 | return "Token expired";
|
---|
[dfd5d87] | 140 | }
|
---|
| 141 |
|
---|
| 142 | confirmationTokenService.setConfirmedAt(token);
|
---|
| 143 | userService.enableUser(confirmationToken.getUser().getEmail());
|
---|
| 144 |
|
---|
| 145 | return "Email confirmed";
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | }
|
---|