source: Git/src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java@ 7bc8942

main
Last change on this file since 7bc8942 was 7bc8942, checked in by Petar Partaloski <ppartaloski@…>, 2 years ago

Fixed password encoding

  • Property mode set to 100644
File size: 2.8 KB
Line 
1package com.wediscussmovies.project.service.impl;
2
3import com.wediscussmovies.project.model.PasswordEncoder;
4import com.wediscussmovies.project.model.User;
5import com.wediscussmovies.project.model.exception.PasswordsDontMatchException;
6import com.wediscussmovies.project.model.exception.UserWithEmailAlreadyExists;
7import com.wediscussmovies.project.model.exception.UserWithUsernameAlreadyExists;
8import com.wediscussmovies.project.repository.UserRepository;
9import com.wediscussmovies.project.service.UserService;
10import org.springframework.http.HttpRequest;
11import org.springframework.stereotype.Service;
12
13import javax.servlet.http.HttpServletRequest;
14import java.security.NoSuchAlgorithmException;
15import java.util.Optional;
16
17@Service
18public class UserServiceImpl implements UserService {
19 private final UserRepository userRepository;
20
21 public UserServiceImpl(UserRepository userRepository) {
22 this.userRepository = userRepository;
23 }
24
25 @Override
26 public Optional<User> login(String email, String password) {
27 Optional<User> userEmail = userRepository.findByEmailAndPassword(email, password);
28 Optional<User> userUsername = userRepository.findByUsernameAndPassword(email, password);
29 if(userEmail.isPresent())
30 return userEmail;
31 else if(userUsername.isPresent())
32 return userUsername;
33 else
34 return Optional.empty();
35 }
36
37 @Override
38 public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword,
39 String username, String name, String surname) {
40 if(email == null || email.isEmpty() || password == null || password.isEmpty() ||
41 confirmPassword == null || confirmPassword.isEmpty() || username == null || username.isEmpty() ||
42 name == null || name.isEmpty() || surname == null || surname.isEmpty()) {
43 request.getSession().setAttribute("error", "Not all of the fields had a value in them, check again.");
44 return Optional.empty();
45 }
46 if(userRepository.findByUsername(username).isPresent()){
47 request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage());
48 return Optional.empty();
49 }
50 if(userRepository.findByEmail(email).isPresent()){
51 request.getSession().setAttribute("error", new UserWithEmailAlreadyExists(email).getMessage());
52 return Optional.empty();
53 }
54 if(!password.equals(confirmPassword)){
55 request.getSession().setAttribute("error", new PasswordsDontMatchException().getMessage());
56 return Optional.empty();
57 }
58 User user = new User(email, username, password, name, surname);
59 return Optional.of(userRepository.save(user));
60 }
61}
Note: See TracBrowser for help on using the repository browser.