Skip to content

Commit

Permalink
Refactor: 로그인 방식 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yso8296 committed Nov 15, 2024
1 parent 866bae3 commit fbc832b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public enum Gender {
}

public static Gender fromString(String gender) {
if (gender.equals("male")) {
if (gender.equals("FEMALE")) {
return MALE;
}
return FEMALE;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/hackathon/nullnullteam/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public class Member extends BaseTimeEntity {

@Enumerated(EnumType.STRING)
private AnxietyLevel anxietyLevel;

private String profile;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hackathon.nullnullteam.global.annotation.Authenticate;
import com.hackathon.nullnullteam.global.dto.GlobalResponse;
import com.hackathon.nullnullteam.member.controller.dto.MemberRequest;
import com.hackathon.nullnullteam.member.controller.dto.MemberResponse;
import com.hackathon.nullnullteam.member.service.MemberService;
import com.hackathon.nullnullteam.member.service.dto.MemberModel;
Expand All @@ -10,8 +11,10 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -31,10 +34,8 @@ public ResponseEntity<Void> login() {
}*/

@GetMapping("/callback")
public ResponseEntity<MemberResponse.Login> registerMember(
@RequestParam("token") String token
) {
MemberModel.Login memberInfo = memberService.register(token);
public ResponseEntity<MemberResponse.Login> registerMember(@RequestBody MemberRequest.Info request) {
MemberModel.Login memberInfo = memberService.register(request.toCommand());


return ResponseEntity.status(HttpStatus.CREATED)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hackathon.nullnullteam.member.controller.dto;

import com.hackathon.nullnullteam.member.Gender;
import com.hackathon.nullnullteam.member.service.dto.MemberCommand;
import lombok.Builder;

public class MemberRequest {

@Builder
public record Info(
String name,
String gender,
int age,
String profile,
String email
) {

public MemberCommand.Info toCommand() {
return MemberCommand.Info.builder()
.name(name)
.gender(gender)
.age(age)
.profile(profile)
.email(email)
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hackathon.nullnullteam.member.infrastructure.apicaller.dto.KakaoAccount;
import com.hackathon.nullnullteam.member.infrastructure.apicaller.dto.TokenInfoResponse;
import com.hackathon.nullnullteam.member.infrastructure.apicaller.dto.UserInfoResponse;
import com.hackathon.nullnullteam.member.service.dto.MemberCommand;
import com.hackathon.nullnullteam.member.service.dto.MemberModel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -26,18 +27,18 @@ public String getCodeUrl() {
}

@Transactional
public MemberModel.Login register(String token) {
// 토큰 발급
public MemberModel.Login register(MemberCommand.Info command) {
/* // 토큰 발급
String kakaoToken = token;
// 카카오 사용자 정보 요청
UserInfoResponse userInfoResponse = memberApiCaller.extractUserInfo(kakaoToken);
KakaoAccount kakaoAccount = userInfoResponse.kakaoAccount();
KakaoAccount kakaoAccount = userInfoResponse.kakaoAccount();*/

// Users 저장 및 중복 체크
Member member = memberReaderService.findByEmail(kakaoAccount.email())
Member member = memberReaderService.findByEmail(command.email())
.orElseGet(() -> {
Member newMember = memberWriterService.saveMember(userInfoResponse);
Member newMember = memberWriterService.saveMember(command);
return newMember;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.hackathon.nullnullteam.member.infrastructure.apicaller.dto.KakaoAccount;
import com.hackathon.nullnullteam.member.infrastructure.apicaller.dto.UserInfoResponse;
import com.hackathon.nullnullteam.member.infrastructure.repository.MemberRepository;
import com.hackathon.nullnullteam.member.service.dto.MemberCommand;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -18,14 +19,15 @@ public class MemberWriterService {
private final MemberRepository memberRepository;

@Transactional
public Member saveMember(UserInfoResponse userInfoResponse) {
KakaoAccount kakaoAccount = userInfoResponse.kakaoAccount();
public Member saveMember(MemberCommand.Info command) {
// KakaoAccount kakaoAccount = userInfoResponse.kakaoAccount();
Member member = memberRepository.save(
Member.builder()
.name(kakaoAccount.name())
.email(kakaoAccount.email())
.age(LocalDate.now().getYear() - Integer.parseInt(kakaoAccount.birthYear()))
.gender(Gender.fromString(kakaoAccount.gender()))
.name(command.name())
.age(command.age())
.gender(Gender.fromString(command.gender()))
.profile(command.profile())
.email(command.email())
.build());
return member;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.hackathon.nullnullteam.member.service.dto;

import lombok.Builder;

public class MemberCommand {

@Builder
public record Info(
String name,
String gender,
int age,
String profile,
String email
) {


}
}

0 comments on commit fbc832b

Please sign in to comment.