Skip to content

Commit

Permalink
feat(#30): 로그인 API 구현
Browse files Browse the repository at this point in the history
- 로그인 기능을 구현했어요.
- 로그인시 유저가 존재하지 않거나, 비밀번호가 틀리면 예외가 발생해요.
  • Loading branch information
cabbage16 committed Nov 27, 2024
1 parent 4910015 commit 8170833
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TokenResponse> logIn(
@RequestBody @Valid LogInRequest request
) {
return CommonResponse.ok(
logInUseCase.execute(request)
);
}

@GetMapping("/google/web/link")
public SingleCommonResponse<String> getGoogleAuthUrl() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8170833

Please sign in to comment.