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

소셜 로그인 구현 #59

Merged
merged 1 commit into from
Aug 4, 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
11 changes: 11 additions & 0 deletions src/main/java/kimandhong/oxox/dto/user/SocialLoginDto.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package kimandhong.oxox.dto.user;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;

public record SocialLoginDto(
@Email
@NotBlank(message = "email is required")
@Schema(example = "[email protected]")
String email,
@NotBlank(message = "displayName is required")
@Schema(example = "김민호")
String displayName,
String photoUrl,
@NotBlank(message = "uid is required")
@Schema(example = "test uid")
String uid
) {
}
2 changes: 2 additions & 0 deletions src/main/java/kimandhong/oxox/handler/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum ErrorCode {
BAD_REQUEST_LOGIN(HttpStatus.BAD_REQUEST, "아이디 또는 비밀번호를 확인해주세요"),
WRONG_PASSWORD(HttpStatus.BAD_REQUEST, "비밀번호를 확인해주세요."),
WRONG_PARAMETER(HttpStatus.BAD_REQUEST, "정렬 종류를 확인해주세요."),
INVALID_UID(HttpStatus.BAD_REQUEST, "잘못된 UID입니다."),

UNAUTHORIZED_REQUEST(HttpStatus.UNAUTHORIZED, "Unauthorized."),
FORBIDDEN_ACCESS(HttpStatus.FORBIDDEN, "Forbidden."),
Expand All @@ -25,6 +26,7 @@ public enum ErrorCode {

METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "Not allowed method."),

NOT_SOCIAL_USER(HttpStatus.CONFLICT, "Email, Password를 사용해 가입한 사용자입니다."),
CONFLICT(HttpStatus.CONFLICT, "Conflict"),
CONFLICT_EMAIL(HttpStatus.CONFLICT, "중복된 이메일입니다."),
CONFLICT_GOOGLE(HttpStatus.CONFLICT, "Google로 가입된 이메일입니다."),
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/kimandhong/oxox/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public void updatePost(final Long postId, final RequestPostDto postDto, final Mu
public void deletePost(final Long id) {
final Post post = postRepository.findByIdAndUserId(id, securityUtil.getCustomUserId()).orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_POST));
postRepository.deleteById(post.getId());
s3Service.deleteFile(post.getThumbnail());
if (post.getThumbnail() != null) {
s3Service.deleteFile(post.getThumbnail());
}
}

@Transactional
Expand Down
56 changes: 35 additions & 21 deletions src/main/java/kimandhong/oxox/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import kimandhong.oxox.dto.user.LoginDto;
import kimandhong.oxox.dto.user.SocialLoginDto;
import kimandhong.oxox.handler.error.ErrorCode;
import kimandhong.oxox.handler.error.exception.BadRequestException;
import kimandhong.oxox.handler.error.exception.ConflictException;
import kimandhong.oxox.handler.error.exception.NotFoundException;
import kimandhong.oxox.repository.UserRepository;
Expand All @@ -26,38 +27,51 @@ public class UserService {

@Transactional
public User join(final JoinDto joinDto, final MultipartFile file) {
final String profileImage = file != null ? s3Service.uploadFile(file, S3path.PROFILE) : null;
userRepository.findByEmail(joinDto.email()).ifPresent(user -> {
throw new ConflictException(user.getUid() == null
? ErrorCode.CONFLICT_EMAIL
: ErrorCode.CONFLICT_GOOGLE);
});

try {
userRepository.findByEmail(joinDto.email()).ifPresent(user -> {
throw new ConflictException(user.getUid() == null
? ErrorCode.CONFLICT_EMAIL
: ErrorCode.CONFLICT_GOOGLE);
});
final String password = passwordEncoder.encode(joinDto.password());
final Long sequence = profileCustomRepository.findMaxSequenceByNickname(joinDto.nickname()) + 1;

final String password = passwordEncoder.encode(joinDto.password());
final Long sequence = profileCustomRepository.findMaxSequenceByNickname(joinDto.nickname()) + 1;
final String profileImage = file != null ? s3Service.uploadFile(file, S3path.PROFILE) : null;

final User user = User.from(joinDto, password, sequence, profileImage);
final User user = User.from(joinDto, password, sequence, profileImage);

return userRepository.save(user);
} catch (Exception e) {
s3Service.deleteFile(profileImage);
throw new RuntimeException(e.getMessage());
}
return userRepository.save(user);
}

public User login(final LoginDto loginDto) {
return userRepository.findByEmail(loginDto.email())
.filter(foundUser -> passwordEncoder.matches(loginDto.password(), foundUser.getPassword()))
.map(user -> {
if (user.getPassword() == null) {
throw new BadRequestException(ErrorCode.CONFLICT_GOOGLE);
}
if (!passwordEncoder.matches(loginDto.password(), user.getPassword())) {
throw new NotFoundException(ErrorCode.BAD_REQUEST_LOGIN);
}
return user;
})
.orElseThrow(() -> new NotFoundException(ErrorCode.BAD_REQUEST_LOGIN));
}

public User socialLogin(final SocialLoginDto loginDto) {
return userRepository.findByUid(loginDto.uid()).orElseGet(() -> {
final Long sequence = profileCustomRepository.findMaxSequenceByNickname(loginDto.displayName()) + 1;
final User user = User.from(loginDto, sequence);
return userRepository.save(user);
});
return userRepository.findByEmail(loginDto.email())
.map(user -> {
if (!user.getPassword().isEmpty()) {
throw new BadRequestException(ErrorCode.NOT_SOCIAL_USER);
}
if (!user.getUid().equals(loginDto.uid())) {
throw new BadRequestException(ErrorCode.INVALID_UID);
}
return user;
})
.orElseGet(() -> {
final Long sequence = profileCustomRepository.findMaxSequenceByNickname(loginDto.displayName()) + 1;
final User newUser = User.from(loginDto, sequence);
return userRepository.save(newUser);
});
}
}