package com.izouma.immall.security;

import com.izouma.immall.domain.BackUser;
import com.izouma.immall.domain.User;
import com.izouma.immall.repo.BackUserRepo;
import lombok.AllArgsConstructor;
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;

@AllArgsConstructor
@Service("jwtBackUserDetailsService")
public class JwtBackUserDetailsService implements UserDetailsService {
    private BackUserRepo backUserRepo;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        BackUser user = backUserRepo.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
        } else {
            return JwtBackUserFactory.create(user);
        }
    }
}
