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

edit : OAuth 로그인 프로세스 변경 #169

Merged
merged 15 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -2,12 +2,8 @@

public record OAuthUserInfo(
String email,
String name,
String nickname,
String imageUrl,
String birthYear,
String ageRange,
String gender,
String provider,
String providerId
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
@RequiredArgsConstructor
public class OAuth2LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

private final String DOMAIN = "https://team-civilwar-boardsignal-fe.pages.dev";
private final String DOMAIN = "http://localhost:5173";
// "https://team-civilwar-boardsignal-fe.pages.dev";
private final String REFRESHTOKEN_NAME = "RefreshToken_Id";

private final AuthService authService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ public final class AuthApiMapper {
public static UserLoginRequest toUserLoginRequest(OAuthUserInfo oAuthUserInfo) {
return new UserLoginRequest(
oAuthUserInfo.email(),
oAuthUserInfo.name(),
oAuthUserInfo.nickname(),
oAuthUserInfo.imageUrl(),
oAuthUserInfo.birthYear(),
oAuthUserInfo.ageRange(),
oAuthUserInfo.gender(),
oAuthUserInfo.provider(),
oAuthUserInfo.providerId()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,14 @@ private static OAuthUserInfo kakaoUserInfo(Map<String, Object> userAttributes,
Map<String, Object> profile = (Map<String, Object>) kakaoAccount.get("profile");

String email = (String) kakaoAccount.get("email");
String name = (String) kakaoAccount.get("name");
String nickName = (String) profile.get("nickname");
String imageUrl = (String) profile.get("profile_image_url");
String birthYear = (String) kakaoAccount.get("birthyear");
String ageRange = (String) kakaoAccount.get("age_range");
String gender = (String) kakaoAccount.get("gender");
String providerId = userAttributes.get("id").toString();

return new OAuthUserInfo(
email,
name,
nickName,
imageUrl,
birthYear,
ageRange,
gender,
provider,
providerId
);
Expand All @@ -54,26 +46,18 @@ private static OAuthUserInfo kakaoUserInfo(Map<String, Object> userAttributes,
private static OAuthUserInfo naverUserInfo(Map<String, Object> userAttributes,
String provider) {

Map<String, String> kakaoAccount = (Map<String, String>) userAttributes
Map<String, String> naverAccount = (Map<String, String>) userAttributes
.get("response");

String email = kakaoAccount.get("email");
String name = kakaoAccount.get("name");
String nickName = kakaoAccount.get("nickname");
String imageUrl = kakaoAccount.get("profile_image");
String birthYear = kakaoAccount.get("birthyear");
String ageRange = kakaoAccount.get("age");
String gender = kakaoAccount.get("gender");
String providerId = kakaoAccount.get("id");
String email = naverAccount.get("email");
String nickName = naverAccount.get("nickname");
String imageUrl = naverAccount.get("profile_image");
String providerId = naverAccount.get("id");

return new OAuthUserInfo(
email,
name,
nickName,
imageUrl,
birthYear,
ageRange,
gender,
provider,
providerId
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public record ApiUserModifyRequest(
String nickName,
String gender,
int birth,
String line,
String station,
List<String> categories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static UserModifyRequest toUserModifyRequest(Long userId,
userId,
apiUserModifyRequest.nickName(),
userCategories,
apiUserModifyRequest.gender(),
apiUserModifyRequest.birth(),
apiUserModifyRequest.line(),
apiUserModifyRequest.station(),
image
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.civilwar.boardsignal.auth.presentation;

import static org.mockito.BDDMockito.given;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand All @@ -10,21 +9,18 @@
import com.civilwar.boardsignal.auth.domain.model.Token;
import com.civilwar.boardsignal.auth.exception.AuthErrorCode;
import com.civilwar.boardsignal.auth.infrastructure.JwtTokenProvider;
import com.civilwar.boardsignal.boardgame.domain.constant.Category;
import com.civilwar.boardsignal.common.support.ApiTestSupport;
import com.civilwar.boardsignal.user.UserFixture;
import com.civilwar.boardsignal.user.domain.constants.Role;
import com.civilwar.boardsignal.user.domain.entity.User;
import com.civilwar.boardsignal.user.domain.repository.UserRepository;
import jakarta.servlet.http.Cookie;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Supplier;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.transaction.annotation.Transactional;

@DisplayName("[AuthApiController 테스트]")
class AuthApiControllerTest extends ApiTestSupport {
Expand Down Expand Up @@ -95,41 +91,6 @@ void logoutTest2() throws Exception {
.andExpect(jsonPath("$.logoutResult").value(false));
}

@Test
@Transactional
@DisplayName("[AccessToken의 사용자 정보를 반환한다.]")
void getLoginUserInfoTest() throws Exception {
//given
User loginUserEntity = userRepository.findById(loginUser.getId())
.orElseThrow();

String updateNickname = "업데이트된 닉네임";
List<Category> categories = List.of(Category.CUSTOMIZABLE, Category.PARTY);
String subwayLine = "2호선";
String subwayStation = "사당역";
String image = "update image";
loginUserEntity.updateUser(updateNickname, categories, subwayLine, subwayStation, image);

LocalDateTime now = LocalDateTime.of(2024, 3, 4, 9, 27, 0);
given(nowTime.get()).willReturn(now);

int expectedAge = nowTime.get().getYear() - loginUser.getBirth() + 1;

//then
mockMvc.perform(get("/api/v1/auth")
.header(AUTHORIZATION, accessToken))
.andExpect(jsonPath("$.id").value(loginUser.getId()))
.andExpect(jsonPath("$.email").value(loginUser.getEmail()))
.andExpect(jsonPath("$.nickname").value(updateNickname))
.andExpect(jsonPath("$.ageGroup").value(loginUser.getAgeGroup().getDescription()))
.andExpect(jsonPath("$.gender").value(loginUser.getGender().getDescription()))
.andExpect(jsonPath("$.isJoined").value(true))
.andExpect(jsonPath("$.age").value(expectedAge))
.andExpect(jsonPath("$.subwayLine").value(subwayLine))
.andExpect(jsonPath("$.subwayStation").value(subwayStation))
.andExpect(jsonPath("$.categories.size()").value(2));
}

@Test
@DisplayName("[인증이 안 된 사용자가 접근 시 401 에러와 전용 에러 메시지 & 코드를 반환한다.]")
void exceptionHandlingTest() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.civilwar.boardsignal.room.dto.response.ParticipantJpaDto;
import com.civilwar.boardsignal.room.infrastructure.repository.MeetingInfoJpaRepository;
import com.civilwar.boardsignal.user.UserFixture;
import com.civilwar.boardsignal.user.domain.constants.AgeGroup;
import com.civilwar.boardsignal.user.domain.constants.Gender;
import com.civilwar.boardsignal.user.domain.constants.Role;
import com.civilwar.boardsignal.user.domain.entity.User;
Expand Down Expand Up @@ -781,14 +780,10 @@ void participantRoomTest3() throws Exception {
roomRepository.save(room);

User user = User.of("email",
"name",
"nickName",
"provider",
"providerId",
"testURL",
1998,
AgeGroup.TWENTY,
Gender.MALE);
"testURL");
User savedUser = userRepository.save(user);

Token token = tokenProvider.createToken(savedUser.getId(), Role.USER);
Expand Down Expand Up @@ -829,14 +824,10 @@ void participantRoomTest4() throws Exception {
roomRepository.save(room);

User user = User.of("email",
"name",
"nickName",
"provider",
"providerId",
"testURL",
2020,
AgeGroup.TWENTY,
Gender.MALE);
"testURL");
User savedUser = userRepository.save(user);

Token token = tokenProvider.createToken(savedUser.getId(), Role.USER);
Expand Down Expand Up @@ -877,14 +868,10 @@ void participantRoomTest5() throws Exception {
roomRepository.save(room);

User user = User.of("email",
"name",
"nickName",
"provider",
"providerId",
"testURL",
1998,
AgeGroup.TWENTY,
Gender.MALE);
"testURL");
User savedUser = userRepository.save(user);

blackListRepository.save(RoomBlackList.of(room.getId(), savedUser.getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.civilwar.boardsignal.review.domain.entity.ReviewEvaluation;
import com.civilwar.boardsignal.review.domain.repository.ReviewRepository;
import com.civilwar.boardsignal.user.UserFixture;
import com.civilwar.boardsignal.user.domain.constants.AgeGroup;
import com.civilwar.boardsignal.user.domain.constants.Gender;
import com.civilwar.boardsignal.user.domain.constants.OAuthProvider;
import com.civilwar.boardsignal.user.domain.constants.Role;
import com.civilwar.boardsignal.user.domain.entity.User;
Expand Down Expand Up @@ -61,6 +63,8 @@ void joinUserTest() throws Exception {

ApiUserModifyRequest apiUserModifyRequest = new ApiUserModifyRequest(
"injuning",
Gender.MALE.getDescription(),
2000,
"2호선",
"사당역",
List.of("가족게임", "파티게임")
Expand Down Expand Up @@ -202,7 +206,8 @@ void validNicknameTest1() throws Exception {
void validNicknameTest2() throws Exception {
//given
String existName = loginUser.getName();
loginUser.updateUser(existName, List.of(Category.CUSTOMIZABLE), "2호선", "사당역", "testURL");
loginUser.updateUser(existName, List.of(Category.CUSTOMIZABLE), Gender.MALE, 2000,
AgeGroup.TWENTY, "2호선", "사당역", "testURL");
userRepository.save(loginUser);
ValidNicknameRequest validNicknameRequest = new ValidNicknameRequest(existName);

Expand Down Expand Up @@ -240,7 +245,8 @@ void validNicknameTest4() throws Exception {
User anotherUser = UserFixture.getUserFixture("providerId", "testURL");
String existName = anotherUser.getName();
userRepository.save(anotherUser);
anotherUser.updateUser(existName, List.of(Category.CUSTOMIZABLE), "2호선", "사당역", "testURL");
anotherUser.updateUser(existName, List.of(Category.CUSTOMIZABLE), Gender.MALE, 2000,
AgeGroup.TWENTY, "2호선", "사당역", "testURL");
userRepository.save(loginUser);

ValidNicknameRequest validNicknameRequest = new ValidNicknameRequest(existName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import com.civilwar.boardsignal.auth.dto.response.UserLoginResponse;
import com.civilwar.boardsignal.auth.dto.response.UserLogoutResponse;
import com.civilwar.boardsignal.auth.mapper.AuthMapper;
import com.civilwar.boardsignal.user.domain.constants.AgeGroup;
import com.civilwar.boardsignal.user.domain.constants.Gender;
import com.civilwar.boardsignal.user.domain.entity.User;
import com.civilwar.boardsignal.user.domain.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -33,14 +31,10 @@ public UserLoginResponse login(UserLoginRequest userLoginRequest) {
user = userRepository.save(
User.of(
userLoginRequest.email(),
userLoginRequest.name(),
userLoginRequest.nickname(),
userLoginRequest.provider(),
userLoginRequest.providerId(),
userLoginRequest.imageUrl(),
Integer.parseInt(userLoginRequest.birthYear()),
AgeGroup.of(userLoginRequest.ageRange(), userLoginRequest.provider()),
Gender.of(userLoginRequest.gender(), userLoginRequest.provider())
userLoginRequest.imageUrl()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

public record UserLoginRequest(
String email,
String name,
String nickname,
String imageUrl,
String birthYear,
String ageRange,
String gender,
String provider,
String providerId
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.civilwar.boardsignal.common.exception.NotFoundException;
import com.civilwar.boardsignal.image.domain.ImageRepository;
import com.civilwar.boardsignal.room.domain.repository.RoomRepository;
import com.civilwar.boardsignal.user.domain.constants.AgeGroup;
import com.civilwar.boardsignal.user.domain.constants.Gender;
import com.civilwar.boardsignal.user.domain.entity.User;
import com.civilwar.boardsignal.user.domain.entity.UserCategory;
import com.civilwar.boardsignal.user.domain.repository.UserRepository;
Expand Down Expand Up @@ -50,6 +52,9 @@ public UserModifyResponse modifyUser(UserModifyRequest userModifyRequest) {
findUser.updateUser(
userModifyRequest.nickName(),
userModifyRequest.categories(),
Gender.toGender(userModifyRequest.gender()),
userModifyRequest.birth(),
AgeGroup.convert(userModifyRequest.birth(), now.get()),
userModifyRequest.line(),
userModifyRequest.station(),
profileImageUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.civilwar.boardsignal.user.exception.UserErrorCode.NOT_FOUND_AGE_GROUP;

import com.civilwar.boardsignal.common.exception.NotFoundException;
import java.time.LocalDateTime;
import java.util.Arrays;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -11,21 +12,23 @@
@RequiredArgsConstructor
public enum AgeGroup {

UNDER_CHILDREN("1~9", "1-9", "미취학 아동"),
CHILDREN("10~14", "10-14", "어린이"),
TEENAGER("14~19", "14-19", "청소년"),
TWENTY("20~29", "20-29", "20대"),
THIRTY("30~39", "30-39", "30대"),
FORTY("40~49", "40-49", "40대"),
FIFTY("50~59", "50-59", "50대"),
SIXTY("60~69", "60-69", "60대"),
SEVENTY("70~79", "70-79", "70대"),
EIGHTY("80~89", "80-89", "80대"),
NINETY("90~", "90-", "90이상");
UNKNOWN("0", "0", "알 ㅅ 없음", 0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오타 있습니다~

UNDER_CHILDREN("1~9", "1-9", "미취학 아동", 9),
CHILDREN("10~14", "10-14", "어린이", 14),
TEENAGER("14~19", "14-19", "청소년", 19),
TWENTY("20~29", "20-29", "20대", 29),
THIRTY("30~39", "30-39", "30대", 39),
FORTY("40~49", "40-49", "40대", 49),
FIFTY("50~59", "50-59", "50대", 59),
SIXTY("60~69", "60-69", "60대", 69),
SEVENTY("70~79", "70-79", "70대", 79),
EIGHTY("80~89", "80-89", "80대", 89),
NINETY("90~", "90-", "90이상", 100);

private final String kakaoType;
private final String naverType;
private final String description;
private final int maxAge;

public static AgeGroup of(String input, String provider) {
return Arrays.stream(values())
Expand All @@ -34,6 +37,20 @@ public static AgeGroup of(String input, String provider) {
.orElseThrow(() -> new NotFoundException(NOT_FOUND_AGE_GROUP));
}

public static AgeGroup convert(int birthYear, LocalDateTime now) {
AgeGroup userAgeGroup = UNKNOWN;
int age = now.getYear() - birthYear + 1;

for (AgeGroup a : values()) {
//현재 연령대의 사용자의 나이가 포함된다면
if (a.getMaxAge() >= age) {
userAgeGroup = a;
}
}
return userAgeGroup;

}

private boolean isEqual(String input, String provider) {
if (provider.equals(OAuthProvider.KAKAO.getType())) {
return input.equalsIgnoreCase(this.kakaoType);
Expand Down
Loading
Loading