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

[FEAT] 로그인 API 버전 구분 개발 #53

Merged
merged 1 commit into from
Nov 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.wable.www.WableServer.api.auth.dto.request.AuthRequestDto;
import com.wable.www.WableServer.api.auth.dto.response.AuthResponseDto;
import com.wable.www.WableServer.api.auth.dto.response.AuthResponseDtoVer2;
import com.wable.www.WableServer.api.auth.dto.response.AuthTokenResponseDto;
import com.wable.www.WableServer.api.auth.service.AuthService;
import com.wable.www.WableServer.common.config.jwt.JwtTokenProvider;
Expand All @@ -20,14 +21,14 @@
import static com.wable.www.WableServer.common.response.SuccessStatus.*;

@RestController
@RequestMapping("/api/v1/auth")
@RequestMapping("/api/")
@RequiredArgsConstructor
@Tag(name="유저 가입과 시큐리티 관련",description = "Auth Api Document")
public class AuthController {
private final AuthService authService;
private final JwtTokenProvider jwtTokenProvider;

@PostMapping
@PostMapping("v1/auth")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "소셜 로그인 및 회원가입",description = "social signup, signin")
public ResponseEntity<ApiResponse<AuthResponseDto>> socialLogin(@RequestHeader("Authorization") String socialAccessToken, @RequestBody AuthRequestDto authRequestDto) throws NoSuchAlgorithmException, InvalidKeySpecException {
Expand All @@ -42,7 +43,7 @@ public ResponseEntity<ApiResponse<AuthResponseDto>> socialLogin(@RequestHeader(

}

@GetMapping("/token")
@GetMapping("v1/auth/token")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "토큰 재발급 API",description = "token")
public ResponseEntity<ApiResponse<AuthTokenResponseDto>> getNewToken(HttpServletRequest request) {
Expand All @@ -51,4 +52,19 @@ public ResponseEntity<ApiResponse<AuthTokenResponseDto>> getNewToken(HttpServlet

return ApiResponse.success(GET_NEW_TOKEN_SUCCESS, authService.getNewToken(accessToken, refreshToken));
}

@PostMapping("v2/auth")
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "소셜 로그인 및 회원가입(+어드민)",description = "social signup, signin with admin")
public ResponseEntity<ApiResponse<AuthResponseDtoVer2>> socialLoginWithAdmin(@RequestHeader("Authorization") String socialAccessToken, @RequestBody AuthRequestDto authRequestDto) throws NoSuchAlgorithmException, InvalidKeySpecException {

AuthResponseDtoVer2 responseDto = authService.socialLoginWithAdmin(socialAccessToken, authRequestDto);
// 로그인
if (!responseDto.getIsNewUser()) {
return ApiResponse.success(SIGNIN_SUCCESS, responseDto);
}
// 회원가입
return ApiResponse.success(SIGNUP_SUCCESS, responseDto);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ public class AuthResponseDto {
private int memberLckYears;

private int memberLevel;

private Boolean isAdmin;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.wable.www.WableServer.api.auth.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
public class AuthResponseDtoVer2 {

private String nickName;

private Long memberId;

private String accessToken;

private String refreshToken;

private String memberProfileUrl;

private Boolean isNewUser;

private Boolean isPushAlarmAllowed;

private String memberFanTeam;

private int memberLckYears;

private int memberLevel;

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

import com.wable.www.WableServer.api.auth.dto.request.AuthRequestDto;
import com.wable.www.WableServer.api.auth.dto.response.AuthResponseDto;
import com.wable.www.WableServer.api.auth.dto.response.AuthResponseDtoVer2;
import com.wable.www.WableServer.api.auth.dto.response.AuthTokenResponseDto;

import java.security.NoSuchAlgorithmException;
Expand All @@ -13,6 +14,8 @@ public interface AuthService {

//새로운 토큰 발급
AuthTokenResponseDto getNewToken(String accessToken, String refreshToken);

AuthResponseDtoVer2 socialLoginWithAdmin(String socialAccessToken, AuthRequestDto authRequestDto) throws NoSuchAlgorithmException, InvalidKeySpecException;
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.wable.www.WableServer.api.auth.SocialPlatform;
import com.wable.www.WableServer.api.auth.dto.SocialInfoDto;
import com.wable.www.WableServer.api.auth.dto.response.AuthResponseDto;
import com.wable.www.WableServer.api.auth.dto.response.AuthResponseDtoVer2;
import com.wable.www.WableServer.api.auth.dto.response.AuthTokenResponseDto;
import com.wable.www.WableServer.api.auth.dto.request.AuthRequestDto;
import com.wable.www.WableServer.api.auth.service.AppleAuthService;
Expand Down Expand Up @@ -76,11 +77,9 @@ public AuthResponseDto socialLogin(String socialAccessToken, AuthRequestDto auth

int memberLevel = MemberUtil.refineMemberExpToLevel(member.getMemberExp());

boolean isAdmin = isAdmin(member.getId());

return AuthResponseDto.of(member.getNickname(), member.getId(), accessToken, refreshToken, member.getProfileUrl(),
true, member.getIsPushAlarmAllowed(), member.getMemberFanTeam(), member.getMemberLckYears(),
memberLevel, isAdmin);
memberLevel);

}
else {
Expand All @@ -103,11 +102,9 @@ public AuthResponseDto socialLogin(String socialAccessToken, AuthRequestDto auth

int signedMemberLevel = MemberUtil.refineMemberExpToLevel(signedMember.getMemberExp());

boolean isAdmin = isAdmin(signedMember.getId());

return AuthResponseDto.of(signedMember.getNickname(), signedMember.getId(), accessToken,
refreshToken, signedMember.getProfileUrl(), false, signedMember.getIsPushAlarmAllowed(),
signedMember.getMemberFanTeam(), signedMember.getMemberLckYears(), signedMemberLevel, isAdmin);
signedMember.getMemberFanTeam(), signedMember.getMemberLckYears(), signedMemberLevel);
}
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException(ErrorStatus.ANOTHER_ACCESS_TOKEN.getMessage());
Expand Down Expand Up @@ -143,6 +140,78 @@ private SocialInfoDto getSocialData(SocialPlatform socialPlatform, String social
}
}

@Override
@Transactional
public AuthResponseDtoVer2 socialLoginWithAdmin(String socialAccessToken, AuthRequestDto authRequestDto) throws NoSuchAlgorithmException, InvalidKeySpecException {

val socialPlatform = SocialPlatform.valueOf(authRequestDto.getSocialPlatform());
SocialInfoDto socialData = getSocialData(socialPlatform, socialAccessToken, authRequestDto.getUserName());
String refreshToken = jwtTokenProvider.generateRefreshToken();
Boolean isExistUser = isMemberBySocialId(socialData.getId());


try {
// 신규 유저 저장
if (!isExistUser.booleanValue()) {
Member member = Member.builder()
.nickname(DEFAULT_NICKNAME)//.nickname(socialData.getNickname())
.socialPlatform(socialPlatform)
.socialId(socialData.getId())
.profileUrl("")
.memberEmail(socialData.getEmail())
.socialNickname(socialData.getNickname())
.build();
memberRepository.save(member);

if (Objects.equals(environment.getProperty("spring.config.activate.on-profile"), "prod")){
slackService.sendSlackMessage(memberRepository.count(), "#wable-signup");
}

Authentication authentication = new UserAuthentication(member.getId(), null, null);

String accessToken = jwtTokenProvider.generateAccessToken(authentication);
member.updateRefreshToken(refreshToken);

int memberLevel = MemberUtil.refineMemberExpToLevel(member.getMemberExp());

boolean isAdmin = isAdmin(member.getId());

return AuthResponseDtoVer2.of(member.getNickname(), member.getId(), accessToken, refreshToken, member.getProfileUrl(),
true, member.getIsPushAlarmAllowed(), member.getMemberFanTeam(), member.getMemberLckYears(),
memberLevel, isAdmin);

}
else {

Boolean isDeleted = memberRepository.findMemberBySocialId(socialData.getId()).isDeleted();

//재가입 방지
if(isExistUser && isDeleted){
throw new BadRequestException(ErrorStatus.WITHDRAWAL_MEMBER.getMessage());
}

findMemberBySocialId(socialData.getId()).updateRefreshToken(refreshToken);

// socialId를 통해서 등록된 유저 찾기
Member signedMember = findMemberBySocialId(socialData.getId());

Authentication authentication = new UserAuthentication(signedMember.getId(), null, null);

String accessToken = jwtTokenProvider.generateAccessToken(authentication);

int signedMemberLevel = MemberUtil.refineMemberExpToLevel(signedMember.getMemberExp());

boolean isAdmin = isAdmin(signedMember.getId());

return AuthResponseDtoVer2.of(signedMember.getNickname(), signedMember.getId(), accessToken,
refreshToken, signedMember.getProfileUrl(), false, signedMember.getIsPushAlarmAllowed(),
signedMember.getMemberFanTeam(), signedMember.getMemberLckYears(), signedMemberLevel, isAdmin);
}
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException(ErrorStatus.ANOTHER_ACCESS_TOKEN.getMessage());
}
}

private boolean isAdmin(Long memberId) {
List<Long> allowedIds = adminConfig.getAllowedIds();
return allowedIds.contains(memberId);
Expand Down
Loading