Changeset f25d07e for phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/RegistrationService.java
- Timestamp:
- 09/07/22 00:51:50 (2 years ago)
- Branches:
- master
- Children:
- 527b93f
- Parents:
- dbd4834
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/RegistrationService.java
rdbd4834 rf25d07e 1 1 package finki.it.phoneluxbackend.services; 2 2 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; 3 7 import finki.it.phoneluxbackend.data.RegistrationRequest; 4 8 import finki.it.phoneluxbackend.data.UserRole; … … 8 12 import finki.it.phoneluxbackend.security.email.EmailValidator; 9 13 import lombok.AllArgsConstructor; 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; 10 18 import org.springframework.stereotype.Service; 11 19 import org.springframework.transaction.annotation.Transactional; 12 20 13 21 import java.time.LocalDateTime; 22 import java.util.HashMap; 14 23 15 24 @Service … … 22 31 23 32 24 public Stringregister(RegistrationRequest request) {33 public ResponseEntity<Object> register(RegistrationRequest request) { 25 34 boolean isValidEmail = emailValidator.test(request.getEmail()); 26 35 27 // validacija za mejl na frontend ?36 // mail is validated on frontend already 28 37 if (!isValidEmail) 29 38 throw new IllegalStateException("Email"+request.getEmail()+" not valid!"); 30 39 31 String token= userService.signUpUser(40 ResponseEntity response = userService.signUpUser( 32 41 new User(request.getFirstName(), 33 42 request.getLastName(), … … 36 45 UserRole.USER)); 37 46 38 String link = "http://localhost:8080/registration/confirm?token="+token; 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]; 39 54 emailSender.send(request.getEmail(), buildEmail(request.getFirstName(),link)); 40 return token; 55 56 return response; 41 57 } 42 58 … … 113 129 @Transactional 114 130 public String confirmToken(String token) { 115 ConfirmationToken confirmationToken = confirmationTokenService.getToken(token) 116 .orElseThrow(() -> new IllegalStateException("Token not found!")); 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!"; 117 139 118 140 if(confirmationToken.getConfirmedAt() != null) 119 throw new IllegalStateException("Email already confirmed!");141 return "Email already confirmed!"; 120 142 121 143 LocalDateTime expiresAt = confirmationToken.getExpiresAt(); 122 144 123 145 if (expiresAt.isBefore(LocalDateTime.now())){ 124 throw new IllegalStateException("Token expired");146 return "Token expired"; 125 147 } 126 148
Note:
See TracChangeset
for help on using the changeset viewer.