Index: src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java
===================================================================
--- src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java	(revision 3ded84dfbf8ff25bf27cc822ac97b84a063fa95a)
+++ src/main/java/com/wediscussmovies/project/service/impl/UserServiceImpl.java	(revision e0ef1b13a29101c3f7a56f68da8dcdc8cce98b76)
@@ -1,58 +1,53 @@
 package com.wediscussmovies.project.service.impl;
 
-import com.wediscussmovies.project.model.*;
-import com.wediscussmovies.project.model.exception.PasswordsDontMatchException;
-import com.wediscussmovies.project.model.exception.UserWithEmailAlreadyExists;
-import com.wediscussmovies.project.model.exception.UserWithUsernameAlreadyExists;
+import com.wediscussmovies.project.model.exception.InvalidArgumentsException;
+import com.wediscussmovies.project.model.exception.PasswordsDoNotMatchException;
+import com.wediscussmovies.project.model.exception.UserNotExistException;
+import com.wediscussmovies.project.model.User;
+import com.wediscussmovies.project.model.exception.UsernameAlreadyExistsException;
 import com.wediscussmovies.project.repository.UserRepository;
 import com.wediscussmovies.project.service.UserService;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.stereotype.Service;
-
-import javax.servlet.http.HttpServletRequest;
-import java.util.Optional;
 
 @Service
 public class UserServiceImpl implements UserService {
+
     private final UserRepository userRepository;
+    private final PasswordEncoder passwordEncoder;
 
-    public UserServiceImpl(UserRepository userRepository) {
+
+    public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
         this.userRepository = userRepository;
+        this.passwordEncoder = passwordEncoder;
     }
 
     @Override
-    public Optional<User> login(String email, String password) {
-        Optional<User> userEmail = userRepository.findByEmailAndPassword(email, password);
-        Optional<User> userUsername = userRepository.findByUsernameAndPassword(email, password);
-        if(userEmail.isPresent())
-            return userEmail;
-        else if(userUsername.isPresent())
-            return userUsername;
-        else
-            return Optional.empty();
+    public User findByUsername(String username) {
+        return this.userRepository.findByUsername(username).orElseThrow(() -> new UserNotExistException(username));
+    }
+
+
+
+    @Override
+    public User register( String email, String username, String password, String confirmPassword
+                                  , String name, String surname) {
+
+        if ( username.isEmpty() || password.isEmpty())
+            throw new InvalidArgumentsException();
+        if (!password.equals(confirmPassword))
+            throw new PasswordsDoNotMatchException();
+        if(this.userRepository.findByUsername(username).isPresent())
+            throw new UsernameAlreadyExistsException(username);
+        User user = new User(email,username,passwordEncoder.encode(password),name,surname);
+        return userRepository.save(user);
+
     }
 
     @Override
-    public Optional<User> register(HttpServletRequest request, String email, String password, String confirmPassword,
-                                   String username, String name, String surname) {
-        if(email == null || email.isEmpty() || password == null || password.isEmpty() ||
-                confirmPassword == null || confirmPassword.isEmpty() || username == null || username.isEmpty() ||
-                name == null || name.isEmpty() || surname == null || surname.isEmpty()) {
-            request.getSession().setAttribute("error", "Not all of the fields had a value in them, check again.");
-            return Optional.empty();
-        }
-        if(userRepository.findByUsername(username).isPresent()){
-            request.getSession().setAttribute("error", new UserWithUsernameAlreadyExists(username).getMessage());
-            return Optional.empty();
-        }
-        if(userRepository.findByEmail(email).isPresent()){
-            request.getSession().setAttribute("error", new UserWithEmailAlreadyExists(email).getMessage());
-            return Optional.empty();
-        }
-        if(!password.equals(confirmPassword)){
-            request.getSession().setAttribute("error", new PasswordsDontMatchException().getMessage());
-            return Optional.empty();
-        }
-        User user = new User(email, username, password, name, surname);
-        return Optional.of(userRepository.save(user));
+    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
+        return this.findByUsername(username);
     }
 }
