Ignore:
Timestamp:
09/07/22 00:51:50 (2 years ago)
Author:
Marko <Marko@…>
Branches:
master
Children:
527b93f
Parents:
dbd4834
Message:

Edited registration and login services

File:
1 edited

Legend:

Unmodified
Added
Removed
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/RegistrationService.java

    rdbd4834 rf25d07e  
    11package finki.it.phoneluxbackend.services;
    22
     3import com.fasterxml.jackson.core.JsonParser;
     4import com.fasterxml.jackson.core.io.JsonStringEncoder;
     5import com.fasterxml.jackson.databind.ObjectMapper;
     6import com.fasterxml.jackson.databind.util.JSONPObject;
    37import finki.it.phoneluxbackend.data.RegistrationRequest;
    48import finki.it.phoneluxbackend.data.UserRole;
     
    812import finki.it.phoneluxbackend.security.email.EmailValidator;
    913import lombok.AllArgsConstructor;
     14import org.apache.coyote.Response;
     15import org.apache.tomcat.util.json.JSONParser;
     16import org.springframework.http.HttpStatus;
     17import org.springframework.http.ResponseEntity;
    1018import org.springframework.stereotype.Service;
    1119import org.springframework.transaction.annotation.Transactional;
    1220
    1321import java.time.LocalDateTime;
     22import java.util.HashMap;
    1423
    1524@Service
     
    2231
    2332
    24     public String register(RegistrationRequest request) {
     33    public ResponseEntity<Object> register(RegistrationRequest request) {
    2534        boolean isValidEmail = emailValidator.test(request.getEmail());
    2635
    27         // validacija za mejl na frontend ?
     36        // mail is validated on frontend already
    2837        if (!isValidEmail)
    2938            throw new IllegalStateException("Email"+request.getEmail()+" not valid!");
    3039
    31         String token = userService.signUpUser(
     40        ResponseEntity response = userService.signUpUser(
    3241                new User(request.getFirstName(),
    3342                        request.getLastName(),
     
    3645                        UserRole.USER));
    3746
    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];
    3954        emailSender.send(request.getEmail(), buildEmail(request.getFirstName(),link));
    40         return token;
     55
     56        return response;
    4157    }
    4258
     
    113129    @Transactional
    114130    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!";
    117139
    118140        if(confirmationToken.getConfirmedAt() != null)
    119             throw new IllegalStateException("Email already confirmed!");
     141            return "Email already confirmed!";
    120142
    121143        LocalDateTime expiresAt = confirmationToken.getExpiresAt();
    122144
    123145        if (expiresAt.isBefore(LocalDateTime.now())){
    124             throw new IllegalStateException("Token expired");
     146            return "Token expired";
    125147        }
    126148
Note: See TracChangeset for help on using the changeset viewer.