-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/com/gdschongik/gdsc/domain/auth/api/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.gdschongik.gdsc.domain.auth.api; | ||
|
||
import com.gdschongik.gdsc.domain.auth.application.AuthService; | ||
import com.gdschongik.gdsc.domain.auth.dto.request.LoginRequest; | ||
import com.gdschongik.gdsc.domain.auth.dto.response.LoginResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
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; | ||
|
||
@Tag(name = "Auth", description = "어드민 인증 API입니다.") | ||
@RestController | ||
@RequestMapping("/auth") | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
|
||
private final AuthService authService; | ||
|
||
@Operation(summary = "로그인", description = "로그인을 수행합니다. 어드민만 가능합니다.") | ||
@PostMapping("/login") | ||
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) { | ||
LoginResponse response = authService.loginAdmin(request); | ||
return ResponseEntity.ok().body(response); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/gdschongik/gdsc/domain/auth/application/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.gdschongik.gdsc.domain.auth.application; | ||
|
||
import com.gdschongik.gdsc.domain.auth.dto.request.LoginRequest; | ||
import com.gdschongik.gdsc.domain.auth.dto.response.LoginResponse; | ||
import com.gdschongik.gdsc.domain.member.dao.MemberRepository; | ||
import com.gdschongik.gdsc.domain.member.domain.Member; | ||
import com.gdschongik.gdsc.domain.member.domain.MemberRole; | ||
import com.gdschongik.gdsc.global.exception.CustomException; | ||
import com.gdschongik.gdsc.global.exception.ErrorCode; | ||
import com.gdschongik.gdsc.global.property.SwaggerProperty; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
|
||
private final SwaggerProperty swaggerProperty; | ||
private final JwtService jwtService; | ||
private final MemberRepository memberRepository; | ||
|
||
public LoginResponse loginAdmin(LoginRequest request) { | ||
Member member = memberRepository | ||
.findByEmail(request.email()) | ||
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND)); | ||
|
||
if (member.getRole() != MemberRole.ADMIN) { | ||
log.error("Invalid role"); | ||
throw new CustomException(ErrorCode.INVALID_ROLE); | ||
} | ||
|
||
if (!request.password().equals(swaggerProperty.getPassword())) { | ||
log.error("Invalid password"); | ||
throw new CustomException(ErrorCode.INVALID_PASSWORD); | ||
} | ||
|
||
String accessToken = | ||
jwtService.createAccessToken(member.getId(), member.getRole()).tokenValue(); | ||
String refreshToken = jwtService.createRefreshToken(member.getId()).tokenValue(); | ||
|
||
return LoginResponse.from(accessToken, refreshToken); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/gdschongik/gdsc/domain/auth/dto/request/LoginRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.gdschongik.gdsc.domain.auth.dto.request; | ||
|
||
public record LoginRequest(String email, String password) {} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/gdschongik/gdsc/domain/auth/dto/response/LoginResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.gdschongik.gdsc.domain.auth.dto.response; | ||
|
||
public record LoginResponse(String accessToken, String refreshToken) { | ||
|
||
public static LoginResponse from(String accessToken, String refreshToken) { | ||
return new LoginResponse(accessToken, refreshToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters