package com.upsfj.sfjprojectbackend.service;

import com.upsfj.sfjprojectbackend.entity.auth.AccountBean;
import com.upsfj.sfjprojectbackend.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.User;
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;


@Service
public class AuthorizeService implements UserDetailsService {

     @Resource
    UserMapper mapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username == null)
            throw new UsernameNotFoundException("用户名不能为空");
        AccountBean account = mapper.findAccountByNameOrEmail(username);
//        System.out.println(account);
        if (account == null)
            throw new UsernameNotFoundException("用户名或密码错误");
        return User

                .withUsername(account.getUsername())
                .password(account.getPassword())
                .roles("user")
                .build();


    }
}
