Skip to content

Commit

Permalink
Merge pull request #21 from coffee-meet/feat/#7
Browse files Browse the repository at this point in the history
Feat/#7 OAuth2.0 로그인 구현
  • Loading branch information
1o18z authored Oct 23, 2023
2 parents d9e7f6e + e8fe5fd commit 5588ad0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -28,7 +30,6 @@ public ResponseEntity<Void> redirectAuthCodeRequestUrl(@PathVariable OAuthProvid
HttpServletResponse response) throws IOException {
String redirectUrl = authService.getAuthCodeRequestUrl(oAuthProvider);
response.sendRedirect(redirectUrl);

return new ResponseEntity<>(HttpStatus.FOUND);
}

Expand All @@ -37,4 +38,17 @@ public ResponseEntity<AuthTokens> 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> 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);
}

}
8 changes: 8 additions & 0 deletions src/main/java/coffeemeet/server/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,6 +63,13 @@ public AuthTokens signup(SignupRequest request) {
return authTokensGenerator.generate(newUser.getId());
}

public Optional<AuthTokens> login(OAuthProvider oAuthProvider, String authCode) {
OAuthInfoResponse response = oauthMemberClientComposite.fetch(oAuthProvider, authCode);
Optional<User> 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())) {
Expand Down
24 changes: 3 additions & 21 deletions src/main/java/coffeemeet/server/user/domain/ReportInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,22 @@
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;

@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;
}

}
5 changes: 3 additions & 2 deletions src/main/java/coffeemeet/server/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<User, Long> {
Expand All @@ -10,4 +11,7 @@ boolean existsUserByOauthInfo_oauthProviderAndOauthInfo_oauthProviderId(
OAuthProvider oauthProvider,
String oauthProviderId);

Optional<User> getUserByOauthInfoOauthProviderAndOauthInfoOauthProviderId(
OAuthProvider oAuthProvider,
String authCode);
}

0 comments on commit 5588ad0

Please sign in to comment.