Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 카카오로그인 로컬&배포 분리 #62

Merged
merged 3 commits into from
Feb 18, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
--build-arg SECRET_KEY=${{ secrets.SECRET_KEY }} \
--build-arg KAKAO_API_KEY=${{ secrets.KAKAO_API_KEY }} \
--build-arg KAKAO_REDIRECT_URI=${{ secrets.KAKAO_REDIRECT_URI }} \
--build-arg KAKAO_REDIRECT_URI_LOCAL=${{ secrets.KAKAO_REDIRECT_URI_LOCAL }} \
--build-arg AIPROMPT_DADA=${{ secrets.AIPROMPT_DADA }} \
--build-arg AIPROMPT_CHICHI=${{ secrets.AIPROMPT_DADA }} \
--build-arg AIPROMPT_LULU=${{ secrets.AIPROMPT_DADA }} \
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ ARG SECRET_KEY
ENV SECRET_KEY=${SECRET_KEY}
ARG KAKAO_REDIRECT_URI
ENV KAKAO_REDIRECT_URI=${KAKAO_REDIRECT_URI}
ARG KAKAO_REDIRECT_URI_LOCAL
ENV KAKAO_REDIRECT_URI_LOCAL=${KAKAO_REDIRECT_URI_LOCAL}
ARG KAKAO_API_KEY
ENV KAKAO_API_KEY=${KAKAO_API_KEY}
ARG AIPROMPT_DADA
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/com/kuit/chatdiary/controller/Login/KakaoLogin.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.kuit.chatdiary.dto.login.KakaoLoginResponseDTO;
import com.kuit.chatdiary.service.LogInService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,13 +21,59 @@ public class LogInController {
@Autowired
public LogInService logInService;


@Value("${KAKAO_REDIRECT_URI}")
private String kakaoRedirectUri;

@Value("${KAKAO_REDIRECT_URI_LOCAL}")
private String kakaoRedirectUriLocal;

/*배포용*/
@GetMapping("/kakao/login")
public ResponseEntity<KakaoLoginResponseDTO> login(@RequestParam(value = "code") String code) throws Exception {
//1. 클라이언트에서 로그인 코드를 보내줌 (서버에서 할일 X)
System.out.println("code: "+code);

//2. 토큰 받기
String accessToken = logInService.getAccessToken(code);
String accessToken = logInService.getAccessToken(code, kakaoRedirectUri);
System.out.println("accessToken: "+accessToken);

//3. 사용자 정보 받기
Map<String, Object> userInfo = logInService.getUserInfo(accessToken);
System.out.println("userinfo : "+userInfo);
String nickname = (String)userInfo.get("nickname");
String email = (String) userInfo.get("email");

System.out.println("nickname = " + nickname);
System.out.println("email = " + email);
System.out.println("accessToken = " + accessToken);

String jwt = logInService.generateJwt(email, 3600000);
System.out.println("jwt: "+jwt);

Boolean isMember = logInService.isMember(email);

//가입된 사용자 -> 바로 로그인
if(isMember){
log.info("이미 가입된 사용자입니다.");
}else{//미가입 사용자 -> 회원가입&로그인
log.info("미가입 사용자입니다.");
logInService.saveMember(nickname, email);
}

Long userId = logInService.getUserId(email);
KakaoLoginResponseDTO kakaoLoginResponseDTO = new KakaoLoginResponseDTO(jwt, userId, nickname);
return ResponseEntity.ok().body(kakaoLoginResponseDTO);
}

/*로컬*/
@GetMapping("/kakao/login/local")
public ResponseEntity<KakaoLoginResponseDTO> loginLocal(@RequestParam(value = "code") String code) throws Exception {
//1. 클라이언트에서 로그인 코드를 보내줌 (서버에서 할일 X)
System.out.println("code: "+code);

//2. 토큰 받기
String accessToken = logInService.getAccessToken(code, kakaoRedirectUriLocal);
System.out.println("accessToken: "+accessToken);

//3. 사용자 정보 받기
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/kuit/chatdiary/service/LogInService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,12 @@ public class LogInService {
@Value("${KAKAO_API_KEY}")
private String kakaoApiKey;

@Value("${KAKAO_REDIRECT_URI}")
private String kakaoRedirectUri;

public LogInService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

// AccessToken 발급하는 메서드
public String getAccessToken(String code) {
public String getAccessToken(String code, String redirectUri) {
log.info("[KakaoService.getAccessToken]");

String accessToken = "";
Expand All @@ -60,7 +57,7 @@ public String getAccessToken(String code) {

sb.append("grant_type=authorization_code");
sb.append("&client_id=").append(kakaoApiKey);
sb.append("&redirect_uri=").append(kakaoRedirectUri);
sb.append("&redirect_uri=").append(redirectUri);
sb.append("&code=").append(code);

bw.write(sb.toString());
Expand Down Expand Up @@ -111,9 +108,6 @@ public HashMap<String, Object> getUserInfo(String accessToken) {
String result = responseSb.toString();
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(result);
//
// JsonObject properties = element.getAsJsonObject().get("properties").getAsJsonObject();
// String nickname = properties.getAsJsonObject().get("nickname").getAsString();

JsonObject properties = element.getAsJsonObject().get("properties").getAsJsonObject();
JsonObject kakaoAccount = element.getAsJsonObject().get("kakao_account").getAsJsonObject();
Expand Down
Loading