package wepa.wepa.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import wepa.wepa.domain.Person;
import wepa.wepa.repository.PersonRepository;

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private PersonRepository personRepository;

    @Override
    public UserDetails loadUserByUsername(String studentNumber) throws UsernameNotFoundException {
        Person user = personRepository.findByStudentNumber(studentNumber);
        if (user == null) {
            throw new UsernameNotFoundException("No such user: " + studentNumber);
        }

        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (String authority : user.getAuthorities()) {
            
            //simplest fix, better is to refactor authorities to include ROLE_
            authorities.add(new SimpleGrantedAuthority("ROLE_" + authority));
        }

        return new org.springframework.security.core.userdetails.User(
                user.getName(),
                user.getPassword(),
                true,
                true,
                true,
                true,
                authorities);
    }
}

@@ -930,16 +930,46 @@
 (user ==
+ null %7C%7C user.getPassword() ==
  null) %7B

check if password is set during login