-
Notifications
You must be signed in to change notification settings - Fork 4
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
14조 코드리뷰 5회차 #69
Merged
14조 코드리뷰 5회차 #69
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 9 additions & 36 deletions
45
src/main/java/com/ordertogether/team14_be/auth/application/service/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,29 @@ | ||
package com.ordertogether.team14_be.auth.application.service; | ||
|
||
import com.ordertogether.team14_be.auth.JwtUtil; | ||
import com.ordertogether.team14_be.auth.application.dto.KakaoUserInfo; | ||
import com.ordertogether.team14_be.auth.presentation.KakaoClient; | ||
import com.ordertogether.team14_be.common.web.response.ApiResponse; | ||
import com.ordertogether.team14_be.member.application.service.MemberService; | ||
import com.ordertogether.team14_be.member.persistence.entity.Member; | ||
import java.net.URI; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class AuthService { | ||
|
||
private final KakaoClient kakaoClient; | ||
private final MemberService memberService; | ||
private final JwtUtil jwtUtil; | ||
|
||
@Value(("${FRONT_PAGE_SIGNUP}")) | ||
String redirectPage; | ||
|
||
public ResponseEntity<ApiResponse<String>> kakaoLogin(String authorizationCode) { | ||
String kakaoToken = kakaoClient.getAccessToken(authorizationCode); // 인가코드로부터 카카오토큰 발급 | ||
KakaoUserInfo kakaoUserInfo = kakaoClient.getUserInfo((kakaoToken)); | ||
String userKakaoEmail = kakaoUserInfo.kakaoAccount().email(); // 와 사용자 카카오 이메일이야 | ||
|
||
Optional<Member> existMember = memberService.findMemberByEmail(userKakaoEmail); | ||
if (existMember.isPresent()) { | ||
String serviceToken = | ||
jwtUtil.generateToken(memberService.getMemberId(userKakaoEmail)); // 서비스 토큰 줘야징 | ||
return ResponseEntity.ok((ApiResponse.with(HttpStatus.OK, "로그인 성공", serviceToken))); | ||
} else { | ||
return ResponseEntity.status(HttpStatus.FOUND) | ||
.location( | ||
URI.create(redirectPage + URLEncoder.encode(userKakaoEmail, StandardCharsets.UTF_8))) | ||
.build(); | ||
} | ||
public AuthService(MemberService memberService, JwtUtil jwtUtil) { | ||
this.memberService = memberService; | ||
this.jwtUtil = jwtUtil; | ||
} | ||
|
||
public ResponseEntity<ApiResponse<String>> register( | ||
String email, String deliveryName, String phoneNumber) { | ||
public String register(String email, String deliveryName, String phoneNumber) { | ||
Member member = new Member(email, deliveryName, phoneNumber); | ||
memberService.registerMember(member); | ||
Long memberId = memberService.getMemberId(email); | ||
String serviceToken = jwtUtil.generateToken(memberId); | ||
return ResponseEntity.ok((ApiResponse.with(HttpStatus.OK, "회원가입 및 로그인 성공", serviceToken))); | ||
return serviceToken; | ||
} | ||
|
||
public String getServiceToken(String email) { | ||
return jwtUtil.generateToken(memberService.getMemberId(email)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/ordertogether/team14_be/auth/application/service/KakaoAuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.ordertogether.team14_be.auth.application.service; | ||
|
||
import com.ordertogether.team14_be.auth.application.dto.KakaoUserInfo; | ||
import com.ordertogether.team14_be.auth.presentation.KakaoClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class KakaoAuthService { | ||
private final KakaoClient kakaoClient; | ||
|
||
public String getKakaoUserEmail(String authorizationCode) { | ||
String kakaoToken = kakaoClient.getAccessToken(authorizationCode); | ||
KakaoUserInfo kakaoUserInfo = kakaoClient.getUserInfo((kakaoToken)); | ||
String userKakaoEmail = kakaoUserInfo.kakaoAccount().email(); | ||
return userKakaoEmail; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/ordertogether/team14_be/config/CorsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.ordertogether.team14_be.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class CorsConfig implements WebMvcConfigurer { | ||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry | ||
.addMapping("/**") | ||
.allowedOrigins("*") | ||
.allowedMethods("GET", "POST", "PUT", "DELETE") | ||
.allowedHeaders("Authorization", "Content-Type") | ||
.exposedHeaders("Custom-Header") | ||
.maxAge(3600); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/ordertogether/team14_be/memebr/application/service/MemberService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.ordertogether.team14_be.memebr.application.service; | ||
|
||
import com.ordertogether.team14_be.memebr.persistence.MemberRepository; | ||
import com.ordertogether.team14_be.memebr.persistence.entity.Member; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class MemberService { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
public MemberService(MemberRepository memberRepository) { | ||
this.memberRepository = memberRepository; | ||
} | ||
|
||
public void findOrCreateMember(String email) { | ||
Member member = | ||
memberRepository | ||
.findByEmail(email) | ||
.orElseGet( | ||
() -> { | ||
Member newMember = Member.createMember(email); | ||
return memberRepository.saveAndFlush(newMember); | ||
}); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/ordertogether/team14_be/memebr/persistence/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ordertogether.team14_be.memebr.persistence; | ||
|
||
import com.ordertogether.team14_be.memebr.persistence.entity.Member; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
|
||
Optional<Member> findByEmail(String email); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Frontend와 Backend서버간 Origin이 서로 달라서 CORS 설정을 해주신것 같아요.
서로 같은 Origin이 되도록 웹 서버 (NGINX) 를 하나 두고, 이를 통해 FE의 파일들과, BE의 API를 제공하면 CORS 헤더 없이도 문제를 해결할 수 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오호.. nginx를 활용해서 해결할 수도 있는거였군요,, 좋은 방법 알려주셔서 감사합니다! nginx통해서 https 통신을 해보려고 했는데 이 과정 중에서 적용해보겠습니다..!