Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import or.sopt.houme.domain.user.controller.dto.CustomUserDetails;
import or.sopt.houme.domain.user.controller.dto.KakaoLoginResponse;
import or.sopt.houme.domain.user.service.OAuthService;
import or.sopt.houme.global.api.ApiResponse;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -45,14 +46,15 @@ public void kakaoOAuthCallback(@RequestParam(value = "env", required = false) St

@Operation(summary = "카카오 인증서버 토큰 검증 API",
description = "프론트에서 전달한 code를 이용해 토큰 교환 및 로그인 처리를 수행합니다. <br><br>" +
"액세스 토큰은 헤더에, 리프레시 토큰은 쿠키에 담아 반환합니다.")
"기존회원이면 액세스 토큰은 헤더에, 리프레시 토큰은 쿠키에 담아 반환합니다. <br>" +
"신규회원이면 signupToken(임시토큰)과 prefill 정보를 반환합니다.")
@GetMapping("/oauth/kakao/callback")
public ResponseEntity<ApiResponse<Boolean>> kakaoLogin(@RequestParam("code") String accessCode,
public ResponseEntity<ApiResponse<KakaoLoginResponse>> kakaoLogin(@RequestParam("code") String accessCode,
@RequestParam(value = "env", required = false) String env,
HttpServletRequest request,
HttpServletResponse response) {

Boolean result = (env == null)
KakaoLoginResponse result = (env == null)
? oAuthService.kakaoLogin(accessCode, request, response)
: oAuthService.kakaoLogin(accessCode, env, request, response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import or.sopt.houme.domain.user.controller.dto.*;
import or.sopt.houme.domain.user.entity.Gender;
import or.sopt.houme.domain.user.service.UserDeletionService;
import or.sopt.houme.domain.user.service.UserService;
import or.sopt.houme.domain.user.service.OAuthService;
import or.sopt.houme.global.api.ApiResponse;
import or.sopt.houme.global.api.ErrorCode;
import or.sopt.houme.global.api.GeneralException;
Expand All @@ -25,6 +27,7 @@ public class UserController {

private final UserService userService;
private final UserDeletionService userDeletionService;
private final OAuthService oAuthService;

@GetMapping(value = "/mypage/user") // 유저의 이름, 사용가능한 크레딧 개수 조회
@Operation(summary = "마이페이지 기본 정보 제공 API")
Expand Down Expand Up @@ -72,6 +75,37 @@ public ResponseEntity<ApiResponse<String>> updateUser(@AuthenticationPrincipal C
return ResponseEntity.ok(ApiResponse.ok(username));
}

@PostMapping(value = "/sign-up")
@Operation(summary = "소셜 자체 회원가입 API",
description = "카카오 소셜로그인 완료 후 발급된 signupToken(임시토큰)과 함께 호출하면 회원을 생성합니다. <br><br>" +
"성공 시 access-token 헤더와 refresh-token 쿠키를 함께 반환합니다.")
public ResponseEntity<ApiResponse<String>> signUp(@RequestBody @Valid SocialSignUpRequest signUpRequest,
HttpServletResponse response) {
Gender gender;
LocalDate birthday;

try {
gender = Gender.valueOf(signUpRequest.gender());
} catch (IllegalArgumentException e) {
throw new GeneralException(ErrorCode.NOT_VALID_EXCEPTION);
}
try {
birthday = LocalDate.parse(signUpRequest.birthday());
} catch (IllegalArgumentException e) {
throw new GeneralException(ErrorCode.NOT_VALID_EXCEPTION);
}

String username = oAuthService.signUpWithToken(
signUpRequest.signupToken(),
signUpRequest.name(),
gender,
birthday,
response
);

return ResponseEntity.ok(ApiResponse.ok(username));
}

@DeleteMapping("/user")
@Operation(summary = "회원 탈퇴 API",
description = "회원을 삭제합니다. <br><br>" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package or.sopt.houme.domain.user.controller.dto;

public record KakaoLoginResponse(
boolean isNewUser,
String signupToken,
Prefill prefill
) {
public static KakaoLoginResponse newUser(String signupToken, String email, String nickname) {
return new KakaoLoginResponse(true, signupToken, new Prefill(email, nickname));
}

public static KakaoLoginResponse existingUser() {
return new KakaoLoginResponse(false, null, null);
}

public record Prefill(
String email,
String nickname
) {
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package or.sopt.houme.domain.user.controller.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import or.sopt.houme.domain.user.valid.ValidBirthday;

public record SocialSignUpRequest(
@NotBlank(message = "signupToken은 필수 입력값입니다.")
String signupToken,

@NotBlank(message = "이름은 필수 입력값입니다.")
@Pattern(regexp = "^[가-힣a-zA-Z]+$", message = "숫자, 특수문자는 입력할 수 없어요.")
String name,

@NotBlank(message = "성별은 필수 입력값입니다.")
@Pattern(
regexp = "MALE|FEMALE|NONBINARY",
message = "성별은 MALE, FEMALE, NONBINARY 중 하나여야 해요."
)
String gender,

@NotBlank(message = "생년월일은 필수 입력값입니다.")
@ValidBirthday
String birthday
) {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package or.sopt.houme.domain.user.entity.record;

public record SignupSession(
Long kakaoId,
String email,
String nickname
) {
public static SignupSession of(Long kakaoId, String email, String nickname) {
return new SignupSession(kakaoId, email, nickname);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package or.sopt.houme.domain.user.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import or.sopt.houme.domain.user.entity.record.SignupSession;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.util.Map;
import java.util.Optional;

@Component
@RequiredArgsConstructor
public class SignupSessionRepository {

private final RedisTemplate<String, Object> redisTemplate;
private final ObjectMapper objectMapper;

private static final String PREFIX = "signup-session:";

public void save(String signupToken, SignupSession session, long ttlSeconds) {
if (!StringUtils.hasText(signupToken) || session == null) {
return;
}
String key = PREFIX + signupToken;
redisTemplate.opsForValue().set(key, session, Duration.ofSeconds(ttlSeconds));
}

public Optional<SignupSession> consume(String signupToken) {
if (!StringUtils.hasText(signupToken)) {
return Optional.empty();
}
String key = PREFIX + signupToken;

Object value;
try {
value = redisTemplate.opsForValue().getAndDelete(key);
} catch (Exception e) {
value = redisTemplate.opsForValue().get(key);
redisTemplate.delete(key);
}

if (value instanceof SignupSession session) {
return Optional.of(session);
}
if (value instanceof Map<?, ?> map) {
return Optional.ofNullable(objectMapper.convertValue(map, SignupSession.class));
}
return Optional.empty();
}
}
Loading
Loading