content: package spring.security.service;
  
  import org.hibernate.mapping.Collection;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.stereotype.Service;
  import spring.security.model.Role;
  import spring.security.model.User;
  import spring.security.repisitory.UserRegistrationDto;
  import spring.security.repisitory.UserRepository;
  
  import java.util.Arrays;
  import java.util.stream.Collectors;
  
  /**
   * @Created 29 / 04 / 2020 - 4:39 PM
   * @project SpringRegistor
   * @Author Hamdamboy
   */
  
  @Service
  public class UserServiceImpl implements UserService{
  
      @Autowired
      private UserRepository userRepository;
  
      @Autowired
      private BCryptPasswordEncoder passwordEncoder;
  
      public User findByEmail(String email) {
          return userRepository.findByEmail(email);
      }
  
      public User save(UserRegistrationDto registration) {
          User user = new User();
          user.setFirstName(registration.getFirstName());
          user.setLastName(registration.getLastName());
          user.setEmail(registration.getEmail());
          user.setPassword(passwordEncoder.encode(registration.getPassword()));
          user.setRoles(Arrays.asList(new Role("ROLE_USER")));
          return userRepository.save(user);
      }
  
      @Override
      public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
          User user = userRepository.findByEmail(email);
          if (user == null) {
              throw new UsernameNotFoundException("Invalid username or password.");
          }
          return new org.springframework.security.core.userdetails.User(user.getEmail(),
                  user.getPassword(),
                  mapRolesToAuthorities(user.getRoles()));
      }
  
      private Collection <<? extends GrantedAuthority> mapRolesToAuthorities(Collection <Role> roles) {
          return roles.stream()
                  .map(role -> new SimpleGrantedAuthority(role.getName()))
                  .collect(Collectors.toList());
      }
  }
  