|
| 1 | +package org.ject.support.domain.auth.controller; |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Operation; |
| 4 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 5 | +import jakarta.validation.Valid; |
| 6 | +import jakarta.validation.constraints.NotBlank; |
| 7 | +import lombok.AllArgsConstructor; |
| 8 | +import org.ject.support.common.security.CustomUserDetails; |
| 9 | +import org.ject.support.common.security.jwt.JwtTokenProvider; |
| 10 | +import org.ject.support.domain.member.entity.Member; |
| 11 | +import org.ject.support.domain.member.repository.MemberRepository; |
| 12 | +import org.springframework.context.annotation.Profile; |
| 13 | +import org.springframework.security.core.Authentication; |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestBody; |
| 16 | +import org.springframework.web.bind.annotation.RestController; |
| 17 | + |
| 18 | +@RestController |
| 19 | +@AllArgsConstructor |
| 20 | +@Profile({"local", "dev"}) |
| 21 | +public class DevAuthController { |
| 22 | + |
| 23 | + private final MemberRepository memberRepository; |
| 24 | + private final JwtTokenProvider jwtTokenProvider; |
| 25 | + |
| 26 | + @Operation( |
| 27 | + summary = "access token 발급", |
| 28 | + description = "개발 편의를 위한 access token 발급 API" |
| 29 | + ) |
| 30 | + @PostMapping("/access-token") |
| 31 | + public String getToken(@Valid @RequestBody EmailRequest request) { |
| 32 | + Member member = memberRepository.findByEmail(request.email) |
| 33 | + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 이메일입니다.")); |
| 34 | + Authentication authentication = jwtTokenProvider.createAuthenticationByMember(member); |
| 35 | + CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal(); |
| 36 | + return jwtTokenProvider.createAccessToken(authentication, customUserDetails.getMemberId()); |
| 37 | + } |
| 38 | + |
| 39 | + public record EmailRequest( |
| 40 | + @Schema(description = "회원 이메일", example = "admin@ject.kr") |
| 41 | + @NotBlank String email |
| 42 | + ) {} |
| 43 | +} |
0 commit comments