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