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

[Feature] 구글 소셜 로그인 redirect 추가 #40

Merged
merged 1 commit into from
Feb 25, 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
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.gdsc.hearo.domain.oauth.controller;

import com.gdsc.hearo.domain.member.dto.LoginResponseDto;
import com.gdsc.hearo.domain.member.entity.Member;
import com.gdsc.hearo.domain.oauth.dto.GoogleProfile;
import com.gdsc.hearo.domain.oauth.service.OauthService;
import com.gdsc.hearo.global.common.BaseException;
import com.gdsc.hearo.global.common.BaseResponse;
import com.gdsc.hearo.global.common.BaseResponseStatus;
import com.gdsc.hearo.global.security.JwtUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

@RestController
@CrossOrigin
Expand All @@ -26,20 +23,23 @@ public class OauthController {
public void socialLoginType() {
oauthService.request();
}

@GetMapping(value = "/google/callback")
public BaseResponse<?> googleLogin(@RequestParam(name = "code") String code) {
public RedirectView googleLogin(@RequestParam(name = "code") String code) {

GoogleProfile googleProfile = oauthService.requestGoogleProfile(code);
Member member = oauthService.googleLogin(googleProfile);

String accessToken = jwtUtil.createAccessToken(member.getLoginId());
String refreshToken = jwtUtil.createRefreshToken(member.getLoginId());

RedirectView redirectView = new RedirectView();

try {
GoogleProfile googleProfile = oauthService.requestGoogleProfile(code);
Member member = oauthService.googleLogin(googleProfile);
redirectView.setUrl("http://localhost:3000/login/callback?accessToken={accessToken}&refreshToken={refreshToken}");
redirectView.addStaticAttribute("accessToken", accessToken);
redirectView.addStaticAttribute("refreshToken", refreshToken);

String accessToken = jwtUtil.createAccessToken(member.getLoginId());
String refreshToken = jwtUtil.createRefreshToken(member.getLoginId());
return redirectView;

LoginResponseDto loginResponseDto = new LoginResponseDto(accessToken, refreshToken);
return new BaseResponse<>(BaseResponseStatus.SUCCESS, loginResponseDto);
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.gdsc.hearo.domain.oauth.dto.GoogleOAuthToken;
import com.gdsc.hearo.domain.oauth.dto.GoogleProfile;
import com.gdsc.hearo.domain.oauth.service.social.GoogleOauth;
import com.gdsc.hearo.global.common.BaseException;
import com.gdsc.hearo.global.common.BaseResponseStatus;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -36,7 +34,7 @@ public GoogleProfile requestGoogleProfile(String code) {
return googleOauth.requestGoogleProfile(googleOAuthToken);
}

public Member googleLogin(GoogleProfile googleProfile) throws BaseException {
public Member googleLogin(GoogleProfile googleProfile) {
String email = googleProfile.getEmail();
String googleId = googleProfile.getId();
String username = googleProfile.getName();
Expand Down
Loading