source: phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/UserService.java@ e5b84dc

Last change on this file since e5b84dc was e5b84dc, checked in by Marko <Marko@…>, 2 years ago

Prototype version

  • Property mode set to 100644
File size: 2.5 KB
Line 
1package finki.it.phoneluxbackend.services;
2
3
4import finki.it.phoneluxbackend.entities.User;
5import finki.it.phoneluxbackend.repositories.UserRepository;
6import finki.it.phoneluxbackend.entities.ConfirmationToken;
7import lombok.AllArgsConstructor;
8
9import org.springframework.http.ResponseEntity;
10import org.springframework.security.core.userdetails.UserDetails;
11import org.springframework.security.core.userdetails.UserDetailsService;
12import org.springframework.security.core.userdetails.UsernameNotFoundException;
13import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
14import org.springframework.stereotype.Service;
15
16import java.time.LocalDateTime;
17
18import java.util.UUID;
19
20@Service
21@AllArgsConstructor
22public class UserService implements UserDetailsService {
23 private final UserRepository userRepository;
24 private final BCryptPasswordEncoder bCryptPasswordEncoder;
25 private final ConfirmationTokenService confirmationTokenService;
26
27
28 @Override
29 public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
30 return userRepository.findByEmail(email)
31 .orElseThrow(() -> new UsernameNotFoundException("User with email "+email+" not found!"));
32 }
33
34 public ResponseEntity<Object> signUpUser(User user)
35 {
36 boolean userExists = userRepository.findByEmail(user.getEmail()).isPresent();
37
38
39 if (userExists){
40 User userToRegister = userRepository.findByEmail(user.getEmail()).get();
41 if(userToRegister.getEnabled()) {
42 return ResponseEntity.badRequest().body("Error:Е-маил адресата е веќе зафатена!");
43 }
44 else {
45 return ResponseEntity.badRequest().body("Error:Профилот не е активиран. Потврдете на вашата е-маил адреса!" );
46 }
47 }
48
49 String encodedPassword = bCryptPasswordEncoder.encode(user.getPassword());
50
51 user.setPassword(encodedPassword);
52
53 String token = UUID.randomUUID().toString();
54 ConfirmationToken confirmationToken = new ConfirmationToken(token,
55 LocalDateTime.now(),
56 LocalDateTime.now().plusMinutes(15),
57 user
58 );
59
60 confirmationTokenService.saveConfirmationToken(confirmationToken);
61
62 return ResponseEntity.ok().body("token:"+token);
63 }
64
65 public int enableUser(String email) {
66 return userRepository.enableUser(email);
67 }
68
69
70}
Note: See TracBrowser for help on using the repository browser.