Skip to content

Commit

Permalink
✨ Feat: AuthService 구현 및 UserController에 추가 (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Oct 15, 2023
1 parent 8c81c46 commit af26095
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/com/diareat/diareat/auth/KakaoAuthService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.diareat.diareat.auth;

import com.diareat.diareat.auth.component.KakaoUserInfo;
import com.diareat.diareat.auth.dto.KakaoUserInfoResponse;
import com.diareat.diareat.user.domain.User;
import com.diareat.diareat.user.repository.UserRepository;
import com.diareat.diareat.util.api.ResponseCode;
import com.diareat.diareat.util.exception.UserException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class KakaoAuthService { // 카카오 소셜로그인, 세션 관리는 추후 구현 예정

private final KakaoUserInfo kakaoUserInfo;
private final UserRepository userRepository;

@Transactional(readOnly = true)
public Long isSignedUp(String token) { // 클라이언트가 보낸 token을 이용해 카카오 API에 유저 정보를 요청, 유저가 존재하지 않으면 예외 발생, 존재하면 회원번호 반환
KakaoUserInfoResponse userInfo = kakaoUserInfo.getUserInfo(token);
User user = userRepository.findByKeyCode(userInfo.getId().toString()).orElseThrow(() -> new UserException(ResponseCode.USER_NOT_FOUND));
return user.getId();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.diareat.diareat.user.controller;

import com.diareat.diareat.auth.KakaoAuthService;
import com.diareat.diareat.user.dto.*;
import com.diareat.diareat.user.service.UserService;
import com.diareat.diareat.util.api.ApiResponse;
Expand All @@ -18,6 +19,14 @@
public class UserController {

private final UserService userService;
private final KakaoAuthService kakaoAuthService;

// 카카오 로그인을 위해 회원가입 여부 확인
@Operation(summary = "[로그인] 카카오 로그인을 위해 회원가입 여부 확인", description = "카카오 로그인을 위해 회원가입 여부를 확인합니다.")
@GetMapping("/auth")
public ApiResponse<Long> authCheck(@RequestParam String token) {
return ApiResponse.success(kakaoAuthService.isSignedUp(token), ResponseCode.USER_READ_SUCCESS.getMessage());
}

// 회원정보 저장
@Operation(summary = "[회원가입] 회원정보 저장", description = "신규 회원정보를 저장합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
List<User> findAllByNameContaining(String name); // 회원이 팔로우를 위해 검색한 유저 목록 조회
boolean existsByName(String name); // 회원가입 시 닉네임 중복 확인
Optional<User> findByKeyCode(String keyCode); // 카카오 회원가입 시 중복 확인
}

0 comments on commit af26095

Please sign in to comment.