| 1 | package com.finki.icare.service;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.dto.LoginRequest;
|
|---|
| 4 | import com.finki.icare.dto.LoginResponse;
|
|---|
| 5 | import com.finki.icare.dto.RegisterPatientRequest;
|
|---|
| 6 | import com.finki.icare.dto.RegisterTherapistRequest;
|
|---|
| 7 | import com.finki.icare.enums.UserType;
|
|---|
| 8 | import com.finki.icare.exceptions.ICareException;
|
|---|
| 9 | import com.finki.icare.model.Patient;
|
|---|
| 10 | import com.finki.icare.model.Therapist;
|
|---|
| 11 | import com.finki.icare.model.User;
|
|---|
| 12 | import com.finki.icare.repository.PatientRepository;
|
|---|
| 13 | import com.finki.icare.repository.TherapistRepository;
|
|---|
| 14 | import com.finki.icare.repository.UserRepository;
|
|---|
| 15 | import com.finki.icare.utils.ValidationUtils;
|
|---|
| 16 | import org.springframework.security.crypto.password.PasswordEncoder;
|
|---|
| 17 | import org.springframework.stereotype.Service;
|
|---|
| 18 |
|
|---|
| 19 | @Service
|
|---|
| 20 | public class AuthService {
|
|---|
| 21 |
|
|---|
| 22 | private final UserRepository userRepository;
|
|---|
| 23 | private final PatientRepository patientRepository;
|
|---|
| 24 | private final TherapistRepository therapistRepository;
|
|---|
| 25 | private final PasswordEncoder passwordEncoder;
|
|---|
| 26 | private final JwtService jwtService;
|
|---|
| 27 |
|
|---|
| 28 | public AuthService(UserRepository userRepository,
|
|---|
| 29 | PatientRepository patientRepository,
|
|---|
| 30 | TherapistRepository therapistRepository,
|
|---|
| 31 | PasswordEncoder passwordEncoder,
|
|---|
| 32 | JwtService jwtService) {
|
|---|
| 33 | this.userRepository = userRepository;
|
|---|
| 34 | this.patientRepository = patientRepository;
|
|---|
| 35 | this.therapistRepository = therapistRepository;
|
|---|
| 36 | this.passwordEncoder = passwordEncoder;
|
|---|
| 37 | this.jwtService = jwtService;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | public LoginResponse login(LoginRequest request) {
|
|---|
| 42 | User user = userRepository.findByUsername(request.getUsername())
|
|---|
| 43 | .orElseThrow(() -> ICareException.unauthorized("Invalid credentials"));
|
|---|
| 44 |
|
|---|
| 45 | // if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
|
|---|
| 46 | // throw ICareException.unauthorized("Invalid credentials");
|
|---|
| 47 | // }
|
|---|
| 48 |
|
|---|
| 49 | String userType;
|
|---|
| 50 | if (patientRepository.existsById(user.getIdUser())) {
|
|---|
| 51 | userType = "PATIENT";
|
|---|
| 52 | } else if (therapistRepository.existsById(user.getIdUser())) {
|
|---|
| 53 | userType = "THERAPIST";
|
|---|
| 54 | } else {
|
|---|
| 55 | throw ICareException.unauthorized("Invalid user.");
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | String token = jwtService.generateToken(user.getUsername(), user.getIdUser(), userType);
|
|---|
| 59 |
|
|---|
| 60 | return new LoginResponse(
|
|---|
| 61 | token,
|
|---|
| 62 | user.getUsername(),
|
|---|
| 63 | user.getEmail(),
|
|---|
| 64 | userType,
|
|---|
| 65 | user.getIdUser()
|
|---|
| 66 | );
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | public LoginResponse registerPatient(RegisterPatientRequest request) {
|
|---|
| 70 | String username = request.getUsername() != null ? request.getUsername().trim() : null;
|
|---|
| 71 | String email = request.getEmail() != null ? request.getEmail().trim() : null;
|
|---|
| 72 | String name = request.getName() != null ? request.getName().trim() : null;
|
|---|
| 73 | String surname = request.getSurname() != null ? request.getSurname().trim() : null;
|
|---|
| 74 |
|
|---|
| 75 | ValidationUtils.validateUsername(username);
|
|---|
| 76 |
|
|---|
| 77 | if (userRepository.findByUsername(username).isPresent()) {
|
|---|
| 78 | throw ICareException.conflict("Username already exists");
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | if (userRepository.findByEmail(email).isPresent()) {
|
|---|
| 82 | throw ICareException.conflict("Email already exists");
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | ValidationUtils.validatePassword(request.getPassword());
|
|---|
| 86 |
|
|---|
| 87 | Patient patient = new Patient();
|
|---|
| 88 | patient.setUsername(username);
|
|---|
| 89 | patient.setPassword(passwordEncoder.encode(request.getPassword()));
|
|---|
| 90 | patient.setName(name);
|
|---|
| 91 | patient.setSurname(surname);
|
|---|
| 92 | patient.setEmail(email);
|
|---|
| 93 |
|
|---|
| 94 | Patient savedPatient = patientRepository.save(patient);
|
|---|
| 95 |
|
|---|
| 96 | String token = jwtService.generateToken(savedPatient.getUsername(), savedPatient.getIdUser(), "PATIENT");
|
|---|
| 97 |
|
|---|
| 98 | return new LoginResponse(
|
|---|
| 99 | token,
|
|---|
| 100 | savedPatient.getUsername(),
|
|---|
| 101 | savedPatient.getEmail(),
|
|---|
| 102 | "PATIENT",
|
|---|
| 103 | savedPatient.getIdUser()
|
|---|
| 104 | );
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | public LoginResponse registerTherapist(RegisterTherapistRequest request) {
|
|---|
| 108 | String username = request.getUsername() != null ? request.getUsername().trim() : null;
|
|---|
| 109 | String email = request.getEmail() != null ? request.getEmail().trim() : null;
|
|---|
| 110 | String name = request.getName() != null ? request.getName().trim() : null;
|
|---|
| 111 | String surname = request.getSurname() != null ? request.getSurname().trim() : null;
|
|---|
| 112 | String officeLocation = request.getOfficeLocation() != null ? request.getOfficeLocation().trim() : null;
|
|---|
| 113 | String degree = request.getDegree() != null ? request.getDegree().trim() : null;
|
|---|
| 114 | String phoneNumber = request.getPhoneNumber() != null ? request.getPhoneNumber().trim() : null;
|
|---|
| 115 |
|
|---|
| 116 | ValidationUtils.validateUsername(username);
|
|---|
| 117 |
|
|---|
| 118 | if (userRepository.findByUsername(username).isPresent()) {
|
|---|
| 119 | throw ICareException.conflict("Username already exists");
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | if (userRepository.findByEmail(email).isPresent()) {
|
|---|
| 123 | throw ICareException.conflict("Email already exists");
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | ValidationUtils.validatePassword(request.getPassword());
|
|---|
| 127 |
|
|---|
| 128 | Therapist therapist = new Therapist();
|
|---|
| 129 | therapist.setUsername(username);
|
|---|
| 130 | therapist.setPassword(passwordEncoder.encode(request.getPassword()));
|
|---|
| 131 | therapist.setName(name);
|
|---|
| 132 | therapist.setSurname(surname);
|
|---|
| 133 | therapist.setEmail(email);
|
|---|
| 134 | therapist.setOfficeLocation(officeLocation);
|
|---|
| 135 | therapist.setDegree(degree);
|
|---|
| 136 | therapist.setYearsExp(request.getYearsExp());
|
|---|
| 137 | therapist.setPhoneNumber(phoneNumber);
|
|---|
| 138 |
|
|---|
| 139 | Therapist savedTherapist = therapistRepository.save(therapist);
|
|---|
| 140 |
|
|---|
| 141 | String token = jwtService.generateToken(savedTherapist.getUsername(), savedTherapist.getIdUser(), UserType.THERAPIST);
|
|---|
| 142 |
|
|---|
| 143 | return new LoginResponse(
|
|---|
| 144 | token,
|
|---|
| 145 | savedTherapist.getUsername(),
|
|---|
| 146 | savedTherapist.getEmail(),
|
|---|
| 147 | UserType.THERAPIST,
|
|---|
| 148 | savedTherapist.getIdUser()
|
|---|
| 149 | );
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|