58 lines
2.4 KiB
Java
58 lines
2.4 KiB
Java
|
|
package com.dog.dognation.config;
|
||
|
|
|
||
|
|
import com.dog.dognation.auth.dto.LoginResponse;
|
||
|
|
import com.dog.dognation.auth.dto.MemberResponse;
|
||
|
|
import com.dog.dognation.common.exception.ApiException;
|
||
|
|
import com.dog.dognation.domain.auth.AuthSession;
|
||
|
|
import com.dog.dognation.domain.auth.AuthSessionRepository;
|
||
|
|
import com.dog.dognation.domain.user.User;
|
||
|
|
import com.dog.dognation.domain.user.UserRepository;
|
||
|
|
import org.springframework.context.annotation.Profile;
|
||
|
|
import org.springframework.http.HttpStatus;
|
||
|
|
import org.springframework.transaction.annotation.Transactional;
|
||
|
|
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 java.time.Duration;
|
||
|
|
import java.time.OffsetDateTime;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.UUID;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 개발자 로그인 — <b>local 프로파일 전용</b>. 구글/애플 없이 이메일로 세션을 발급한다.
|
||
|
|
* 관리자 콘솔을 로컬에서 확인하기 위한 용도(예: admin@dog.com). 운영 빌드에는 등록되지 않는다.
|
||
|
|
*/
|
||
|
|
@Profile("local")
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/api/auth")
|
||
|
|
public class DevAuthController {
|
||
|
|
|
||
|
|
private static final Duration TTL = Duration.ofDays(30);
|
||
|
|
|
||
|
|
private final UserRepository userRepository;
|
||
|
|
private final AuthSessionRepository sessionRepository;
|
||
|
|
|
||
|
|
public DevAuthController(UserRepository userRepository, AuthSessionRepository sessionRepository) {
|
||
|
|
this.userRepository = userRepository;
|
||
|
|
this.sessionRepository = sessionRepository;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 이메일로 세션 발급 (검증 없음, local 전용). */
|
||
|
|
@PostMapping("/dev-login")
|
||
|
|
@Transactional
|
||
|
|
public LoginResponse devLogin(@RequestBody Map<String, String> body) {
|
||
|
|
String email = body.get("email");
|
||
|
|
if (email == null || email.isBlank()) {
|
||
|
|
throw new ApiException(HttpStatus.BAD_REQUEST, "email 이 필요합니다.");
|
||
|
|
}
|
||
|
|
User user = userRepository.findByEmail(email)
|
||
|
|
.orElseThrow(() -> new ApiException(HttpStatus.NOT_FOUND, "회원을 찾을 수 없습니다: " + email));
|
||
|
|
|
||
|
|
String token = UUID.randomUUID().toString().replace("-", "");
|
||
|
|
sessionRepository.save(AuthSession.issue(token, user, true, OffsetDateTime.now().plus(TTL)));
|
||
|
|
return new LoginResponse(token, TTL.getSeconds(), MemberResponse.from(user));
|
||
|
|
}
|
||
|
|
}
|