server/src/main/java/com/example/server/service/AuthService.java
package com.example.server.service;

import com.example.server.dto.MemberResponseDto;
import com.example.server.dto.TokenDto;
import com.example.server.entity.MemberEntity;
import com.example.server.dto.MemberRequestDto;
import com.example.server.jwt.TokenProvider;
import com.example.server.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
public class AuthService {
    private final AuthenticationManagerBuilder managerBuilder;
    private final MemberRepository memberRepository;
    private final PasswordEncoder passwordEncoder;
    private final TokenProvider tokenProvider;
    private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName());

    public MemberResponseDto signup(MemberRequestDto requestDto) {
        if (memberRepository.existsByUserId(requestDto.getUserId())) {
            throw new RuntimeException("User Id is already taken!");
        }

        MemberEntity memberEntity = requestDto.toMember(passwordEncoder);
        return MemberResponseDto.of(memberRepository.save(memberEntity));
    }

    public TokenDto login(MemberRequestDto requestDto) {
        UsernamePasswordAuthenticationToken authenticationToken = requestDto.toAuthentication();

        Authentication authentication = managerBuilder.getObject().authenticate(authenticationToken);

        return tokenProvider.generateTokenDto(authentication);
    }
}
