Skip to content

Commit

Permalink
[#72] Fix: redirect uri for login (#86)
Browse files Browse the repository at this point in the history
* fix: Fix error about get all rooms

* fix: Fix error about get all rooms

* feat: Make userstory logic with openai

* fire: Fix out port

* fix: Add environment for openai

* fix: Add check left chance

* fix: Fix error

* fix: Fix error

* feat: Make color

* feat: Make login redirect uri

* fix: Fix error

* refactor: Seperate login port

* fix: Redirect
  • Loading branch information
donggni0712 authored May 19, 2024
1 parent 0b1d807 commit d8a1106
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
39 changes: 37 additions & 2 deletions src/main/java/com/syncd/adapter/in/web/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
import com.syncd.application.service.LoginService;
import com.syncd.dto.TokenDto;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import java.util.Collection;
import java.util.Enumeration;

@RestController
@RequiredArgsConstructor
@RequestMapping("/login/oauth2")
Expand All @@ -20,10 +25,40 @@ public class AuthController {
private final AuthControllerProperties authControllerProperties;

@GetMapping("/code/{registrationId}")
public RedirectView googleLogin(@RequestParam String code, @PathVariable String registrationId, HttpServletResponse response) {
public RedirectView googleLogin(@RequestParam String code,
@PathVariable String registrationId,
HttpServletRequest request,
HttpServletResponse response) {
String url = authControllerProperties.getRedirectUrl();
TokenDto token = socialLoginUsecase.socialLogin(code, registrationId);
String redirectUrl = url + token.accessToken();

Enumeration<String> reqHeaderNames = request.getHeaderNames();

while (reqHeaderNames.hasMoreElements()) {
String headerName = reqHeaderNames.nextElement();
String headerValue = request.getHeader(headerName);
System.out.println(headerName + ": " + headerValue);
}
String cookies = request.getHeader("cookie");
String refererSubstring="";
int refererIndex = cookies.indexOf("referer=");
if (refererIndex != -1) {
// 'referer=' 이후의 부분 추출
refererSubstring = cookies.substring(refererIndex + "referer=".length());

// ';' 이전의 부분 추출 (쿠키가 끝날 때까지)
int semicolonIndex = refererSubstring.indexOf(';');
if (semicolonIndex != -1) {
refererSubstring = refererSubstring.substring(0, semicolonIndex);
}

System.out.println("Referer: " + refererSubstring);
} else {
System.out.println("Referer not found.");
refererSubstring = "https://syncd.i-dear.org/";
}

String redirectUrl = refererSubstring + url + token.accessToken();
return new RedirectView(redirectUrl);
}
}
8 changes: 2 additions & 6 deletions src/main/java/com/syncd/adapter/in/web/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ public class LoginController {
@GetMapping("/login/google")
public RedirectView redirectToGoogleOAuth(HttpServletRequest request) {
String redirectUrl = googleOAuth2Properties.getRedirectUri();
String targetUrl = request.getHeader("Referer")+"login/oauth2/code/google";
if (targetUrl == null || targetUrl.isBlank()) {
// 기본 URL 설정
targetUrl = redirectUrl;
}
System.out.println(targetUrl);
String targetUrl = redirectUrl;
String url = "https://accounts.google.com/o/oauth2/auth" +
"?client_id=70988875044-9nmbvd2suleub4ja095mrh83qbi7140j.apps.googleusercontent.com" +
"&redirect_uri=" + targetUrl +
"&response_type=code" +
"&scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
System.out.println(url);
return new RedirectView(url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public TokenDto socialLogin(String code, String registrationId) {

private String getAccessToken(String authorizationCode, String registrationId) {


MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("code", authorizationCode);
params.add("client_id", clientId);
Expand Down

0 comments on commit d8a1106

Please sign in to comment.