From 8170833b6fc47658cc8102d8b7cf69336d71ee84 Mon Sep 17 00:00:00 2001 From: cabbage16 Date: Wed, 27 Nov 2024 22:44:50 +0900 Subject: [PATCH] =?UTF-8?q?feat(#30):=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20API?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=20-=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EA=B5=AC=ED=98=84=ED=96=88?= =?UTF-8?q?=EC=96=B4=EC=9A=94.=20-=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=EC=8B=9C?= =?UTF-8?q?=20=EC=9C=A0=EC=A0=80=EA=B0=80=20=EC=A1=B4=EC=9E=AC=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EA=B1=B0=EB=82=98,=20=EB=B9=84=EB=B0=80?= =?UTF-8?q?=EB=B2=88=ED=98=B8=EA=B0=80=20=ED=8B=80=EB=A6=AC=EB=A9=B4=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=EA=B0=80=20=EB=B0=9C=EC=83=9D=ED=95=B4?= =?UTF-8?q?=EC=9A=94.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/auth/LogInUseCase.java | 42 +++++++++++++++++++ .../exception/PasswordMismatchException.java | 10 +++++ .../auth/exception/WrongLogInException.java | 10 +++++ .../exception/error/AuthErrorProperty.java | 5 ++- .../presentation/auth/AuthController.java | 11 +++++ .../auth/dto/request/LogInRequest.java | 18 ++++++++ 6 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bamdoliro/sinabro/application/auth/LogInUseCase.java create mode 100644 src/main/java/com/bamdoliro/sinabro/domain/auth/exception/PasswordMismatchException.java create mode 100644 src/main/java/com/bamdoliro/sinabro/domain/auth/exception/WrongLogInException.java create mode 100644 src/main/java/com/bamdoliro/sinabro/presentation/auth/dto/request/LogInRequest.java diff --git a/src/main/java/com/bamdoliro/sinabro/application/auth/LogInUseCase.java b/src/main/java/com/bamdoliro/sinabro/application/auth/LogInUseCase.java new file mode 100644 index 0000000..ffc6065 --- /dev/null +++ b/src/main/java/com/bamdoliro/sinabro/application/auth/LogInUseCase.java @@ -0,0 +1,42 @@ +package com.bamdoliro.sinabro.application.auth; + +import com.bamdoliro.sinabro.domain.auth.exception.PasswordMismatchException; +import com.bamdoliro.sinabro.domain.auth.exception.WrongLogInException; +import com.bamdoliro.sinabro.domain.auth.service.TokenService; +import com.bamdoliro.sinabro.domain.user.domain.User; +import com.bamdoliro.sinabro.domain.user.domain.value.Password; +import com.bamdoliro.sinabro.domain.user.exception.UserNotFoundException; +import com.bamdoliro.sinabro.domain.user.service.UserFacade; +import com.bamdoliro.sinabro.presentation.auth.dto.request.LogInRequest; +import com.bamdoliro.sinabro.presentation.auth.dto.response.TokenResponse; +import com.bamdoliro.sinabro.shared.annotation.UseCase; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@UseCase +public class LogInUseCase { + + private final UserFacade userFacade; + private final TokenService tokenService; + + public TokenResponse execute(LogInRequest request) { + User user; + try { + user = userFacade.getUser(request.getEmail()); + validate(request.getPassword(), user.getPassword()); + } catch (UserNotFoundException | PasswordMismatchException e) { + throw new WrongLogInException(); + } + + return TokenResponse.builder() + .accessToken(tokenService.generateAccessToken(user.getEmail())) + .refreshToken(tokenService.generateRefreshToken(user.getEmail())) + .build(); + } + + private void validate(String actual, Password expected) { + if (!expected.match(actual)) { + throw new PasswordMismatchException(); + } + } +} diff --git a/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/PasswordMismatchException.java b/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/PasswordMismatchException.java new file mode 100644 index 0000000..4898d85 --- /dev/null +++ b/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/PasswordMismatchException.java @@ -0,0 +1,10 @@ +package com.bamdoliro.sinabro.domain.auth.exception; + +import com.bamdoliro.sinabro.domain.auth.exception.error.AuthErrorProperty; +import com.bamdoliro.sinabro.shared.error.SinabroException; + +public class PasswordMismatchException extends SinabroException { + public PasswordMismatchException() { + super(AuthErrorProperty.PASSWORD_MISMATCH); + } +} diff --git a/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/WrongLogInException.java b/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/WrongLogInException.java new file mode 100644 index 0000000..93d27d2 --- /dev/null +++ b/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/WrongLogInException.java @@ -0,0 +1,10 @@ +package com.bamdoliro.sinabro.domain.auth.exception; + +import com.bamdoliro.sinabro.domain.auth.exception.error.AuthErrorProperty; +import com.bamdoliro.sinabro.shared.error.SinabroException; + +public class WrongLogInException extends SinabroException { + public WrongLogInException() { + super(AuthErrorProperty.WRONG_LOGIN); + } +} diff --git a/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/error/AuthErrorProperty.java b/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/error/AuthErrorProperty.java index 9f4ffeb..83bc7e4 100644 --- a/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/error/AuthErrorProperty.java +++ b/src/main/java/com/bamdoliro/sinabro/domain/auth/exception/error/AuthErrorProperty.java @@ -11,7 +11,10 @@ public enum AuthErrorProperty implements ErrorProperty { EXPIRED_TOKEN(HttpStatus.UNAUTHORIZED, "만료된 토큰입니다."), INVALID_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다."), EMPTY_TOKEN(HttpStatus.UNAUTHORIZED, "인증이 필요합니다."), - AUTHORITY_MISMATCH(HttpStatus.FORBIDDEN, "권한이 없습니다."); + AUTHORITY_MISMATCH(HttpStatus.FORBIDDEN, "권한이 없습니다."), + PASSWORD_MISMATCH(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다."), + WRONG_LOGIN(HttpStatus.UNAUTHORIZED, "사용자가 없거나 비밀번호가 일치하지 않습니다.") + ; private final HttpStatus status; private final String message; diff --git a/src/main/java/com/bamdoliro/sinabro/presentation/auth/AuthController.java b/src/main/java/com/bamdoliro/sinabro/presentation/auth/AuthController.java index 50ee93c..c14049c 100644 --- a/src/main/java/com/bamdoliro/sinabro/presentation/auth/AuthController.java +++ b/src/main/java/com/bamdoliro/sinabro/presentation/auth/AuthController.java @@ -3,6 +3,7 @@ import com.bamdoliro.sinabro.application.auth.*; import com.bamdoliro.sinabro.domain.user.domain.User; import com.bamdoliro.sinabro.presentation.auth.dto.request.GoogleTokenRequest; +import com.bamdoliro.sinabro.presentation.auth.dto.request.LogInRequest; import com.bamdoliro.sinabro.presentation.auth.dto.response.TokenResponse; import com.bamdoliro.sinabro.shared.auth.AuthenticationPrincipal; import com.bamdoliro.sinabro.shared.response.CommonResponse; @@ -23,6 +24,16 @@ public class AuthController { private final LogOutUseCase logOutUseCase; private final GoogleAuthWebUseCase googleAuthWebUseCase; private final GoogleAuthAppUseCase googleAuthAppUseCase; + private final LogInUseCase logInUseCase; + + @PostMapping + public SingleCommonResponse logIn( + @RequestBody @Valid LogInRequest request + ) { + return CommonResponse.ok( + logInUseCase.execute(request) + ); + } @GetMapping("/google/web/link") public SingleCommonResponse getGoogleAuthUrl() { diff --git a/src/main/java/com/bamdoliro/sinabro/presentation/auth/dto/request/LogInRequest.java b/src/main/java/com/bamdoliro/sinabro/presentation/auth/dto/request/LogInRequest.java new file mode 100644 index 0000000..1492820 --- /dev/null +++ b/src/main/java/com/bamdoliro/sinabro/presentation/auth/dto/request/LogInRequest.java @@ -0,0 +1,18 @@ +package com.bamdoliro.sinabro.presentation.auth.dto.request; + +import jakarta.validation.constraints.NotBlank; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class LogInRequest { + + @NotBlank(message = "필수값입니다.") + private String email; + + @NotBlank(message = "필수값입니다.") + private String password; +}