Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PC-379] 어드민 모듈 로그인 기능 #34

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions admin/src/main/java/org/yapp/auth/application/AdminRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.yapp.auth.application;

public enum AdminRole {
ADMIN,
ROLE_ADMIN;
}
40 changes: 40 additions & 0 deletions admin/src/main/java/org/yapp/auth/application/AuthService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.yapp.auth.application;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.yapp.auth.AuthToken;
import org.yapp.auth.AuthTokenGenerator;
import org.yapp.auth.application.dto.LoginDto;
import org.yapp.domain.user.User;
import org.yapp.error.code.auth.AuthErrorCode;
import org.yapp.error.exception.ApplicationException;
import org.yapp.user.application.UserService;

@Slf4j
@Service
@RequiredArgsConstructor
public class AuthService {

private final UserService userService;

private final AuthTokenGenerator authTokenGenerator;

@Value("${admin.password}")
private String PWD;

public AuthToken login(LoginDto loginDto) {
String oauthId = loginDto.oauthId();
String password = loginDto.password();

User loginUser = userService.getUserByOauthId(oauthId);

if (password.equals(PWD) && loginUser.getRole().equals(AdminRole.ADMIN.toString())) {
return authTokenGenerator.generate(loginUser.getId(), null,
AdminRole.ROLE_ADMIN.toString());
} else {
throw new ApplicationException(AuthErrorCode.ACCESS_DENIED);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.yapp.auth.application.dto;

public record LoginDto(String oauthId, String password) {

}
28 changes: 28 additions & 0 deletions admin/src/main/java/org/yapp/auth/presentation/AuthController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.yapp.auth.presentation;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yapp.auth.AuthToken;
import org.yapp.auth.application.AuthService;
import org.yapp.auth.presentation.request.LoginRequest;
import org.yapp.util.CommonResponse;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/v1/auth/")
public class AuthController {

private final AuthService authService;

@PostMapping("/login")
public ResponseEntity<CommonResponse<AuthToken>> login(
@RequestBody @Valid LoginRequest loginRequest) {
AuthToken authToken = authService.login(loginRequest.toDto());
return ResponseEntity.ok(CommonResponse.createSuccess(authToken));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.yapp.auth.presentation.request;

import jakarta.validation.constraints.NotNull;
import org.yapp.auth.application.dto.LoginDto;

public record LoginRequest(@NotNull String loginId, @NotNull String password) {

public LoginDto toDto() {
return new LoginDto(loginId, password);
}
}
2 changes: 1 addition & 1 deletion admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring:
profiles:
include: db
include: db, secret
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
@RequiredArgsConstructor
public enum AuthErrorCode implements ErrorCode {
OAUTH_ERROR(HttpStatus.FORBIDDEN, "Oauth Error"),
ACCESS_DENIED(HttpStatus.FORBIDDEN, "권한이 없습니다."),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

에러 메시지 한글로 하는 거 좋은 것 같습니다!
앞으로는 에러메시지 한글로 할까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

에러 메시지가 한글인게 더 명확한 것 같습니다 !

그리고, 에러가 발생한 응답이 전달될때 클라이언트 분들은 저희 서비스만의 에러 코드(ex. 1404, 1402 ...)가 같이 전달되었으면 하는 바람이 있으신 것 같습니다. 한글로 변환하는 김에 저희 서비스 에러코드도 지정하는 것이 어떨까요 ?

;


private final HttpStatus httpStatus;
private final String message;
}