Skip to content

Commit

Permalink
Merge pull request #31 from MEME-UMC/develop
Browse files Browse the repository at this point in the history
feat: 사용자 등록 여부 기능
  • Loading branch information
limjustin authored Mar 29, 2024
2 parents b18cdc3 + 922bf62 commit 6c6eb7a
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
Optional<User> findByEmail(@Param("email") String email);

boolean existsByNickname(String nickname);
}
13 changes: 12 additions & 1 deletion src/main/java/umc/meme/auth/global/auth/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -29,7 +31,7 @@ public BaseResponseDto<AuthResponse.TokenDto> signupArtist(@RequestBody AuthRequ
}

@PostMapping("/api/v1/auth/artist/extra")
public BaseResponseDto signupArtistExtra(@RequestBody AuthRequest.ArtistExtraDto artistExtraDto) throws AuthException {
public BaseResponseDto<?> signupArtistExtra(@RequestBody AuthRequest.ArtistExtraDto artistExtraDto) throws AuthException {
authService.signupArtistExtra(artistExtraDto);
return BaseResponseDto.SuccessResponse(SuccessStatus.ARTIST_EXTRA_JOIN_SUCCESS);
}
Expand All @@ -55,4 +57,13 @@ public BaseResponseDto<?> withdraw(HttpServletRequest request) throws AuthExcept
authService.withdraw(request.getHeader("Authorization"));
return BaseResponseDto.SuccessResponse(SuccessStatus.WITHDRAW_SUCCESS);
}

@GetMapping("/api/v1/check")
public BaseResponseDto<?> checkUserExists(@RequestBody AuthRequest.IdTokenDto idTokenDto) throws AuthException {
AuthResponse.UserInfoDto userInfoDto = authService.isUserExistsFindByEmail(idTokenDto);
if (userInfoDto.isUser())
return BaseResponseDto.SuccessResponse(SuccessStatus.USER_EXISTS, userInfoDto);
else
return BaseResponseDto.SuccessResponse(SuccessStatus.USER_NOT_EXISTS, userInfoDto);
}
}
62 changes: 48 additions & 14 deletions src/main/java/umc/meme/auth/global/auth/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import umc.meme.auth.global.oauth.service.kakao.KakaoAuthService;

import java.time.LocalDate;
import java.util.Optional;
import java.util.stream.Collectors;

import static umc.meme.auth.global.common.status.ErrorStatus.*;
Expand All @@ -49,6 +50,8 @@ public class AuthService {
private final RedisRepository redisRepository;
private final ModelRepository modelRepository;
private final ArtistRepository artistRepository;
private final KakaoAuthService kakaoAuthService;
private final AppleAuthService appleAuthService;

private final static String TOKEN_PREFIX = "Bearer ";

Expand Down Expand Up @@ -144,20 +147,6 @@ public AuthResponse.TokenDto login(User user) throws AuthException {
return tokenDto;
}

private String getUser(String idToken, Provider provider) throws AuthException {
OAuthService oAuthService;

if (provider.equals(KAKAO)) {
oAuthService = new KakaoAuthService(userRepository, redisRepository);
} else if (provider.equals(APPLE)) {
oAuthService = new AppleAuthService(userRepository, redisRepository);
} else {
throw new AuthException(PROVIDER_ERROR);
}

return oAuthService.getUserInfo(idToken);
}

@Transactional
public AuthResponse.TokenDto reissue(AuthRequest.ReissueDto reissueDto) throws AuthException {
String requestAccessToken = reissueDto.getAccess_token();
Expand Down Expand Up @@ -198,6 +187,51 @@ public void withdraw(String requestHeader) throws AuthException {
userRepository.delete(user);
}

// 회원 등록 여부 조회
@Transactional
public AuthResponse.UserInfoDto isUserExistsFindByEmail(AuthRequest.IdTokenDto idTokenDto) {
String email = "";

if (idTokenDto.getProvider() == KAKAO) {
email = kakaoAuthService.getUserInfo(idTokenDto.getId_token());
} else if (idTokenDto.getProvider() == APPLE) {
email = appleAuthService.getUserInfo(idTokenDto.getId_token());
}

// ID 토큰을 파라미터로 받음
// String email = oAuthService.getUserInfo(idToken);
Optional<User> userOptional = userRepository.findByEmail(email);

AuthResponse.UserInfoDto userInfoDto = new AuthResponse.UserInfoDto();

if (userOptional.isPresent()) {
AuthResponse.TokenDto loginDto = login(userOptional.get());

userInfoDto.setUser(true);
userInfoDto.setUserId(userOptional.get().getUserId());
userInfoDto.setAccessToken(loginDto.getAccessToken());
userInfoDto.setRefreshToken(loginDto.getRefreshToken());
} else {
userInfoDto.setUser(false);
}

return userInfoDto;
}

private String getUser(String idToken, Provider provider) throws AuthException {
OAuthService oAuthService;

if (provider.equals(KAKAO)) {
oAuthService = new KakaoAuthService(userRepository, redisRepository);
} else if (provider.equals(APPLE)) {
oAuthService = new AppleAuthService(userRepository, redisRepository);
} else {
throw new AuthException(PROVIDER_ERROR);
}

return oAuthService.getUserInfo(idToken);
}

private AuthResponse.TokenDto generateToken(String username, String authorities) {
AuthResponse.TokenDto tokenDto = jwtTokenProvider.createToken(username, authorities);
saveRefreshToken(tokenDto.getAccessToken(), tokenDto.getRefreshToken());
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/umc/meme/auth/global/auth/dto/AuthRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ public static class ArtistExtraDto {
private Map<DayOfWeek, Times> availableDayOfWeekAndTime;
}

@Data @Builder
@NoArgsConstructor
@AllArgsConstructor
public static class LoginDto {
private String id_token;
private Provider provider;
}

@Data @Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -84,4 +76,12 @@ public static class AccessTokenDto {
public static class RefreshTokenDto {
private String refreshToken;
}

@Data @Builder
@NoArgsConstructor
@AllArgsConstructor
public static class IdTokenDto {
private String id_token;
private Provider provider;
}
}
10 changes: 10 additions & 0 deletions src/main/java/umc/meme/auth/global/auth/dto/AuthResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ public void setType(String type) {
public static class AccessTokenDto {
private String accessToken;
}

@Data @Builder
@NoArgsConstructor
@AllArgsConstructor
public static class UserInfoDto {
private boolean isUser;
private Long userId;
private String accessToken;
private String refreshToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public enum SuccessStatus {
MODEL_JOIN_SUCCESS(HttpStatus.OK, 200, "회원가입이 완료되었습니다"),
ARTIST_JOIN_SUCCESS(HttpStatus.OK, 200, "회원가입이 완료되었습니다"),
ARTIST_EXTRA_JOIN_SUCCESS(HttpStatus.OK, 200, "추가 회원 정보 기입이 완료되었습니다."),
USER_EXISTS(HttpStatus.OK, 200, "사용자 정보가 확인되었습니다."),
USER_NOT_EXISTS(HttpStatus.OK, 200, "등록되지 않은 사용자입니다."),


// AuthController
LOGIN_SUCCESS(HttpStatus.OK,200,"토큰 검증에 성공하였습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.requestMatchers("/api/v1/signup/model").permitAll()
.requestMatchers("/api/v1/signup/artist").permitAll()
.requestMatchers("/api/v1/reissue").permitAll()
.requestMatchers("/api/v1/check").permitAll()

.requestMatchers("/api/v1/auth/artist/extra").permitAll()
.requestMatchers("/api/v1/auth/logout").permitAll()
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/umc/meme/auth/global/jwt/JwtTokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ public Authentication getAuthentication(String accessToken) {
return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities());
}

public long getTokenExpirationTime(String token) {
return getClaims(token).getExpiration().getTime();
}

public boolean isTokenExpired(String token) {
Long now = System.currentTimeMillis();
return getClaims(token).getExpiration().getTime() > now;
}

private String createAccessToken(String username, String authorities, Long now) {
return Jwts.builder()
.setHeaderParam("alg", "HS512")
Expand Down

0 comments on commit 6c6eb7a

Please sign in to comment.