diff --git a/src/main/java/coffeemeet/server/auth/controller/AuthController.java b/src/main/java/coffeemeet/server/auth/controller/AuthController.java index 7ced79a7..77853a38 100644 --- a/src/main/java/coffeemeet/server/auth/controller/AuthController.java +++ b/src/main/java/coffeemeet/server/auth/controller/AuthController.java @@ -6,6 +6,7 @@ import coffeemeet.server.user.domain.OAuthProvider; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -28,7 +30,6 @@ public ResponseEntity redirectAuthCodeRequestUrl(@PathVariable OAuthProvid HttpServletResponse response) throws IOException { String redirectUrl = authService.getAuthCodeRequestUrl(oAuthProvider); response.sendRedirect(redirectUrl); - return new ResponseEntity<>(HttpStatus.FOUND); } @@ -37,4 +38,17 @@ public ResponseEntity signup(@RequestBody SignupRequest request) { return ResponseEntity.ok(authService.signup(request)); } + @GetMapping("/login/{oAuthProvider}") + public ResponseEntity login(@PathVariable OAuthProvider oAuthProvider, + @RequestParam String authCode, HttpServletResponse response) throws IOException { + Optional authTokens = authService.login(oAuthProvider, authCode); + if (authTokens.isPresent()) { + return ResponseEntity.ok(authTokens.get()); + } + + String signupUrl = "/oauth2/auth/sign-up"; + response.sendRedirect(signupUrl); + return new ResponseEntity<>(HttpStatus.FOUND); + } + } diff --git a/src/main/java/coffeemeet/server/auth/service/AuthService.java b/src/main/java/coffeemeet/server/auth/service/AuthService.java index cde3a740..5b0e7b6c 100644 --- a/src/main/java/coffeemeet/server/auth/service/AuthService.java +++ b/src/main/java/coffeemeet/server/auth/service/AuthService.java @@ -16,6 +16,7 @@ import coffeemeet.server.user.repository.UserRepository; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -62,6 +63,13 @@ public AuthTokens signup(SignupRequest request) { return authTokensGenerator.generate(newUser.getId()); } + public Optional login(OAuthProvider oAuthProvider, String authCode) { + OAuthInfoResponse response = oauthMemberClientComposite.fetch(oAuthProvider, authCode); + Optional foundUser = userRepository.getUserByOauthInfoOauthProviderAndOauthInfoOauthProviderId( + response.oAuthProvider(), response.oAuthProviderId()); + return foundUser.map(user -> authTokensGenerator.generate(user.getId())); + } + private void checkDuplicateUser(OAuthInfoResponse response) { if (userRepository.existsUserByOauthInfo_oauthProviderAndOauthInfo_oauthProviderId( response.oAuthProvider(), response.oAuthProviderId())) { diff --git a/src/main/java/coffeemeet/server/user/domain/ReportInfo.java b/src/main/java/coffeemeet/server/user/domain/ReportInfo.java index 1bf0c209..5e920b75 100644 --- a/src/main/java/coffeemeet/server/user/domain/ReportInfo.java +++ b/src/main/java/coffeemeet/server/user/domain/ReportInfo.java @@ -3,13 +3,10 @@ import jakarta.persistence.Column; import jakarta.persistence.Embeddable; import java.time.LocalDateTime; -import lombok.AccessLevel; import lombok.Getter; -import lombok.NoArgsConstructor; @Getter @Embeddable -@NoArgsConstructor(access = AccessLevel.PROTECTED) public class ReportInfo { private static final int REPORT_MIN_COUNT = 0; @@ -17,26 +14,11 @@ public class ReportInfo { @Column(nullable = false) private int reportedCount; - @Column(nullable = false) private LocalDateTime sanctionPeriod; - public ReportInfo(int reportedCount, LocalDateTime sanctionPeriod) { - validateReportedCount(reportedCount); - validateSanctionPeriod(sanctionPeriod); - this.reportedCount = reportedCount; - this.sanctionPeriod = sanctionPeriod; - } - - private void validateReportedCount(int reportedCount) { - if (reportedCount < REPORT_MIN_COUNT) { - throw new IllegalArgumentException("올바르지 않은 신고횟수입니다."); - } - } - - private void validateSanctionPeriod(LocalDateTime sanctionPeriod) { - if (sanctionPeriod == null) { - throw new IllegalArgumentException("올바르지 않은 제재 기간입니다."); - } + public ReportInfo() { + this.reportedCount = REPORT_MIN_COUNT; + this.sanctionPeriod = null; } } diff --git a/src/main/java/coffeemeet/server/user/domain/User.java b/src/main/java/coffeemeet/server/user/domain/User.java index 811aa3d9..ca575d69 100644 --- a/src/main/java/coffeemeet/server/user/domain/User.java +++ b/src/main/java/coffeemeet/server/user/domain/User.java @@ -36,11 +36,10 @@ public class User extends AdvancedBaseEntity { private Profile profile; @ManyToOne - @JoinColumn(name = "chatting_room_id", nullable = false) + @JoinColumn(name = "chatting_room_id") private ChattingRoom chattingRoom; @Embedded - @Column(nullable = false) private Certification certification; @Embedded @@ -56,6 +55,8 @@ public User( ) { this.oauthInfo = oauthInfo; this.profile = profile; + this.certification = new Certification(); + this.reportInfo = new ReportInfo(); } public void updateBusinessCardUrl(String newBusinessCardUrl) { diff --git a/src/main/java/coffeemeet/server/user/repository/UserRepository.java b/src/main/java/coffeemeet/server/user/repository/UserRepository.java index a83cdb69..90da80fb 100644 --- a/src/main/java/coffeemeet/server/user/repository/UserRepository.java +++ b/src/main/java/coffeemeet/server/user/repository/UserRepository.java @@ -2,6 +2,7 @@ import coffeemeet.server.user.domain.OAuthProvider; import coffeemeet.server.user.domain.User; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository { @@ -10,4 +11,7 @@ boolean existsUserByOauthInfo_oauthProviderAndOauthInfo_oauthProviderId( OAuthProvider oauthProvider, String oauthProviderId); + Optional getUserByOauthInfoOauthProviderAndOauthInfoOauthProviderId( + OAuthProvider oAuthProvider, + String authCode); }