Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AuthCommandServiceImpl implements AuthCommandService {
public void signUp(AuthRequestDTO.SignUp request) {
validateSignUp(request);

Member member = memberRepository.save(AuthConverter.toLocalMember(request.email(), request.username(), passwordEncoder.encode(request.password()), request.phoneNumber(), request.gender(), request.birth()));
Member member = memberRepository.save(AuthConverter.toLocalMember(request.email(), request.username(), request.socialId() != null ? passwordEncoder.encode(request.password()) : null, request.phoneNumber(), request.gender(), request.birth()));
if (request.socialId() != null) {
Social social = socialRepository.findById(request.socialId()).orElseThrow(() ->
new SocialException(SocialErrorCode.NOT_FOUND_SOCIAL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
@AllArgsConstructor
public enum AuthErrorCode implements BaseErrorCode {

NOT_FOUND_LOGIN_MEMBER(HttpStatus.NOT_FOUND, "AUTH404_1", "ν•΄λ‹Ή 이메일을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."),
FAIL_AUTH_LOGIN(HttpStatus.UNAUTHORIZED, "AUTH401_1", "일반 λ‘œκ·ΈμΈμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€."),
ALREADY_EXIST_EMAIL(HttpStatus.BAD_REQUEST, "AUTH400_1", "이미 μ‘΄μž¬ν•˜λŠ” μ΄λ©”μΌμž…λ‹ˆλ‹€.")
ALREADY_EXIST_EMAIL(HttpStatus.BAD_REQUEST, "AUTH400_1", "이미 μ‘΄μž¬ν•˜λŠ” μ΄λ©”μΌμž…λ‹ˆλ‹€."),
ONLY_AVAILABLE_SOCIAL(HttpStatus.BAD_REQUEST, "AUTH400_2", "μ†Œμ…œ 둜그인만 κ°€λŠ₯ν•©λ‹ˆλ‹€.")
;
private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import jakarta.servlet.http.HttpServletResponse;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.namul.api.payload.code.BaseErrorCode;
import org.namul.api.payload.code.DefaultResponseErrorCode;
import org.namul.api.payload.code.dto.supports.DefaultResponseErrorReasonDTO;
import org.namul.api.payload.error.exception.ServerApplicationException;
import org.namul.api.payload.writer.FailureResponseWriter;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -51,6 +54,8 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
return;
}
this.successfulAuthentication(request, response, authentication);
} catch (ServerApplicationException e) {
handleServerApplicationException(response, e);
} catch (Exception e) {
handleException(response, e);
}
Expand All @@ -70,7 +75,8 @@ public Authentication attemptAuthentication(HttpServletRequest request) throws A
} catch (IOException e) {
throw new AuthenticationServiceException("Json Parsing Error In Json Filter");
} catch (Exception e) {
throw new AuthenticationServiceException("CustomJsonUsernamePasswordLoginFilter(" + e.getClass() + "): " + e.getMessage());
Throwable throwable = e.getCause();
throw throwable instanceof ServerApplicationException serverApplicationException ? serverApplicationException : new AuthenticationServiceException("CustomJsonUsernamePasswordLoginFilter(" + e.getClass() + "): " + e.getMessage());
}
}

Expand All @@ -93,6 +99,14 @@ private AuthRequestDTO.Login getBodyInRequest(HttpServletRequest request) throws
return om.readValue(content, AuthRequestDTO.Login.class);
}

private void handleServerApplicationException(HttpServletResponse response, ServerApplicationException e) throws IOException {
ObjectMapper om = new ObjectMapper();
DefaultResponseErrorReasonDTO reasonDTO = e.getCode().getReason() instanceof DefaultResponseErrorReasonDTO defaultResponseErrorReasonDTO ? defaultResponseErrorReasonDTO : DefaultResponseErrorCode._UNAUTHORIZED.getReason();
response.setStatus(reasonDTO.getHttpStatus().value());
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
om.writeValue(response.getOutputStream(), failureResponseWriter.onFailure(reasonDTO, e.getMessage()));
}

private void handleException(HttpServletResponse response, Exception e) throws IOException {
ObjectMapper om = new ObjectMapper();
DefaultResponseErrorReasonDTO reasonDTO = AuthErrorCode.FAIL_AUTH_LOGIN.getReason();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.springframework.stereotype.Component;
import org.withtime.be.withtimebe.domain.member.entity.Member;
import org.withtime.be.withtimebe.domain.member.repository.MemberRepository;
import org.withtime.be.withtimebe.global.error.code.MemberErrorCode;
import org.withtime.be.withtimebe.global.error.exception.MemberException;
import org.withtime.be.withtimebe.global.error.code.AuthErrorCode;
import org.withtime.be.withtimebe.global.error.exception.AuthException;
import org.withtime.be.withtimebe.global.security.domain.CustomUserDetails;

@Component
Expand All @@ -19,7 +19,10 @@ public class CustomUserDetailsService implements UserDetailsService {

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Member member = memberRepository.findByEmail(email).orElseThrow(() -> new MemberException(MemberErrorCode.NOT_FOUND));
Member member = memberRepository.findByEmail(email).orElseThrow(() -> new AuthException(AuthErrorCode.NOT_FOUND_LOGIN_MEMBER));
if (member.getPassword() == null) {
throw new AuthException(AuthErrorCode.ONLY_AVAILABLE_SOCIAL);
}
return new CustomUserDetails(member);
}
}