Skip to content

Commit

Permalink
feat: 로그인 성공 시 그룹 ID 응답 추가 (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
ywonchae1 committed Dec 2, 2024
1 parent 9ad2b2c commit a388245
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public record LoginResponse(
String refreshToken,
@NotNull @Schema(description = "회원이 가입한 그룹이 있는지 여부", example = "true")
Boolean isGroupMember,
@Schema(description = "회원이 가입한 그룹의 ID", example = "1") Long groupId,
@Schema(description = "회원의 그룹 역할", example = "MASTER") GroupMemberRole role) {

@Builder
Expand All @@ -24,11 +25,13 @@ public LoginResponse(
String accessToken,
String refreshToken,
Boolean isGroupMember,
Long groupId,
GroupMemberRole role) {
this.memberId = memberId;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.isGroupMember = isGroupMember;
this.groupId = groupId;
this.role = role;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.ioteatime.meonghanyangserver.auth.mapper;

import org.ioteatime.meonghanyangserver.auth.dto.db.LoginWithMemberInfo;
import org.ioteatime.meonghanyangserver.auth.dto.reponse.LoginResponse;
import org.ioteatime.meonghanyangserver.auth.dto.reponse.RefreshResponse;
import org.ioteatime.meonghanyangserver.groupmember.doamin.GroupMemberEntity;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;
import org.ioteatime.meonghanyangserver.member.dto.response.MemberSimpleResponse;

public class AuthResponseMapper {
Expand All @@ -11,13 +12,26 @@ public static MemberSimpleResponse from(Long id, String email) {
}

public static LoginResponse from(
LoginWithMemberInfo memberInfo, String accessToken, String refreshToken) {
MemberEntity member,
GroupMemberEntity groupMember,
String accessToken,
String refreshToken) {
return LoginResponse.builder()
.memberId(memberInfo.memberId())
.memberId(member.getId())
.accessToken(accessToken)
.refreshToken(refreshToken)
.isGroupMember(memberInfo.isGroupMember())
.role(memberInfo.role())
.isGroupMember(true)
.groupId(groupMember.getGroup().getId())
.role(groupMember.getRole())
.build();
}

public static LoginResponse from(MemberEntity member, String accessToken, String refreshToken) {
return LoginResponse.builder()
.memberId(member.getId())
.accessToken(accessToken)
.refreshToken(refreshToken)
.isGroupMember(false)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import jakarta.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import lombok.RequiredArgsConstructor;
import org.ioteatime.meonghanyangserver.auth.dto.db.LoginWithMemberInfo;
import org.ioteatime.meonghanyangserver.auth.dto.reponse.LoginResponse;
import org.ioteatime.meonghanyangserver.auth.dto.request.IssuePasswordRequest;
import org.ioteatime.meonghanyangserver.auth.dto.request.LoginRequest;
Expand All @@ -18,6 +18,7 @@
import org.ioteatime.meonghanyangserver.common.exception.UnauthorizedException;
import org.ioteatime.meonghanyangserver.common.type.AuthErrorType;
import org.ioteatime.meonghanyangserver.common.utils.JwtUtils;
import org.ioteatime.meonghanyangserver.groupmember.doamin.GroupMemberEntity;
import org.ioteatime.meonghanyangserver.groupmember.repository.GroupMemberRepository;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;
import org.ioteatime.meonghanyangserver.member.dto.request.JoinRequest;
Expand All @@ -39,39 +40,48 @@ public class AuthService {
private final GroupMemberRepository deviceRepository;
private final EmailCodeRepository emailCodeRepository;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
private final GroupMemberRepository groupMemberRepository;
private final RefreshTokenRepository refreshTokenRepository;

public LoginResponse login(LoginRequest loginRequest) {
LoginWithMemberInfo memberInfo =
// 회원 조회, 없으면 오류
MemberEntity memberEntity =
memberRepository
.findGroupMemberInfoByEmail(loginRequest.email())
.findByEmail(loginRequest.email())
.orElseThrow(() -> new NotFoundException(AuthErrorType.NOT_FOUND));
// 그룹 가입 정보 조회
Optional<GroupMemberEntity> groupMemberEntity =
groupMemberRepository.findByMemberId(memberEntity.getId());

boolean passwordMatch =
bCryptPasswordEncoder.matches(loginRequest.password(), memberInfo.password());
bCryptPasswordEncoder.matches(loginRequest.password(), memberEntity.getPassword());
if (!passwordMatch) {
throw new BadRequestException(AuthErrorType.PASSWORD_NOT_MATCH);
}

String accessToken =
jwtUtils.generateAccessToken(memberInfo.nickname(), memberInfo.memberId());
jwtUtils.generateAccessToken(memberEntity.getNickname(), memberEntity.getId());
String refreshToken =
jwtUtils.generateRefreshToken(memberInfo.nickname(), memberInfo.memberId());
jwtUtils.generateRefreshToken(memberEntity.getNickname(), memberEntity.getId());

if (accessToken.isEmpty() || refreshToken.isEmpty()) {
throw new NotFoundException(AuthErrorType.TOKEN_NOT_FOUND);
}
RefreshToken refreshTokenEntity =
RefreshToken.builder()
.memberId(memberInfo.memberId())
.memberId(memberEntity.getId())
.refreshToken(refreshToken)
.build();

refreshTokenRepository.save(refreshTokenEntity);
accessToken = jwtUtils.includeBearer(accessToken);
refreshToken = jwtUtils.includeBearer(refreshToken);

return AuthResponseMapper.from(memberInfo, accessToken, refreshToken);
if (groupMemberEntity.isPresent()) {
return AuthResponseMapper.from(
memberEntity, groupMemberEntity.get(), accessToken, refreshToken);
}
return AuthResponseMapper.from(memberEntity, accessToken, refreshToken);
}

public MemberSimpleResponse joinProcess(JoinRequest userDto) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.ioteatime.meonghanyangserver.member.repository;

import java.util.Optional;
import org.ioteatime.meonghanyangserver.auth.dto.db.LoginWithMemberInfo;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;

public interface MemberRepository {
Expand All @@ -14,6 +13,4 @@ public interface MemberRepository {
MemberEntity save(MemberEntity MemberEntity);

Optional<MemberEntity> updateFcmTokenById(Long memberId, String token);

Optional<LoginWithMemberInfo> findGroupMemberInfoByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.ioteatime.meonghanyangserver.member.repository;

import static org.ioteatime.meonghanyangserver.groupmember.doamin.QGroupMemberEntity.groupMemberEntity;
import static org.ioteatime.meonghanyangserver.member.domain.QMemberEntity.memberEntity;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.ioteatime.meonghanyangserver.auth.dto.db.LoginWithMemberInfo;
import org.ioteatime.meonghanyangserver.member.domain.MemberEntity;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -41,23 +38,4 @@ public MemberEntity save(MemberEntity memberEntity) {
public Optional<MemberEntity> updateFcmTokenById(Long memberId, String token) {
return jpaMemberRepository.findById(memberId).map(entity -> entity.updateFcmToken(token));
}

@Override
public Optional<LoginWithMemberInfo> findGroupMemberInfoByEmail(String email) {
return Optional.ofNullable(
queryFactory
.select(
Projections.constructor(
LoginWithMemberInfo.class,
memberEntity.id.as("memberId"),
memberEntity.nickname.as("nickname"),
memberEntity.password.as("password"),
groupMemberEntity.id.isNotNull().as("isGroupMember"),
groupMemberEntity.role.as("role")))
.from(memberEntity)
.leftJoin(groupMemberEntity)
.on(groupMemberEntity.member.eq(memberEntity))
.where(memberEntity.email.eq(email))
.fetchOne());
}
}

0 comments on commit a388245

Please sign in to comment.