package es.uv.garcosda.auth.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import es.uv.garcosda.shared.domain.User;
import reactor.core.publisher.Mono;

@Service
public class CustomUserDetailsService implements ReactiveUserDetailsService {
	
	@Autowired
	UserService us;
	
	@Value("${mailer.data.url}")
	private String data_api;

    @Override
    public Mono<UserDetails> findByUsername(String username) {
    	
    	User u = us.findByEmail(username);
    	return Mono.just(this.toAuthUser(u));
    	
    }
    
    private org.springframework.security.core.userdetails.User toAuthUser(User u){
    	return new org.springframework.security.core.userdetails.User(u.getEmail(), u.getPassword(), 
				  AuthorityUtils.createAuthorityList("user"));
    }
    
}
