source: backend/src/main/java/com/finki/icare/service/AuthService.java

main
Last change on this file was 700e2f9, checked in by 186079 <matej.milevski@…>, 5 days ago

Init

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[700e2f9]1package com.finki.icare.service;
2
3import com.finki.icare.dto.LoginRequest;
4import com.finki.icare.dto.LoginResponse;
5import com.finki.icare.dto.RegisterPatientRequest;
6import com.finki.icare.dto.RegisterTherapistRequest;
7import com.finki.icare.enums.UserType;
8import com.finki.icare.exceptions.ICareException;
9import com.finki.icare.model.Patient;
10import com.finki.icare.model.Therapist;
11import com.finki.icare.model.User;
12import com.finki.icare.repository.PatientRepository;
13import com.finki.icare.repository.TherapistRepository;
14import com.finki.icare.repository.UserRepository;
15import com.finki.icare.utils.ValidationUtils;
16import org.springframework.security.crypto.password.PasswordEncoder;
17import org.springframework.stereotype.Service;
18
19@Service
20public 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}
Note: See TracBrowser for help on using the repository browser.