Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Sion99 committed Dec 5, 2023
2 parents d35ce9e + 98e6ab2 commit 2043bad
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/uspray/uspray/Enums/Authority.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.uspray.uspray.Enums;

public enum Authority {
ROLE_USER, ROLE_ADMIN
ROLE_USER, ROLE_ADMIN, ROLE_GUEST
}
10 changes: 10 additions & 0 deletions src/main/java/com/uspray/uspray/controller/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -44,4 +45,13 @@ public ApiResponseDto<?> setNotificationAgree(
memberService.changeNotificationAgree(user.getUsername(), notificationAgreeDto);
return ApiResponseDto.success(SuccessStatus.CHANGE_PUSH_AGREE_SUCCESS);
}

@PutMapping("/oauth/{name}")
@Operation(summary = "소셜 로그인 회원가입 이름 설정")
public ApiResponseDto<?> setOAuthName(
@Parameter(hidden = true) @AuthenticationPrincipal User user,
@PathVariable("name") String name) {
memberService.changeName(user.getUsername(), name);
return ApiResponseDto.success(SuccessStatus.CHANGE_NAME_SUCCESS);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/uspray/uspray/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public void changeFirebaseToken(String firebaseToken) {
public void changePhone(String phone) {
this.phone = phone;
}
public void changeName(String name) {
this.name = name;
}

public void changeAuthority(Authority authority) {
this.authority = authority;
}

public void changePw(String pw) {
this.password = pw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum SuccessStatus {
REISSUE_SUCCESS(HttpStatus.OK, "토큰 재발급에 성공했습니다."),
PUSH_SUCCESS(HttpStatus.OK, "푸쉬 알림을 성공적으로 전송했습니다."),
CHANGE_PUSH_AGREE_SUCCESS(HttpStatus.OK, "푸쉬 알림 설정을 성공적으로 변경했습니다."),
CHANGE_NAME_SUCCESS(HttpStatus.OK, "이름을 성공적으로 변경했습니다."),
UPDATE_CATEGORY_SUCCESS(HttpStatus.OK, "카테고리 수정에 성공했습니다."),
GET_CATEGORY_SUCCESS(HttpStatus.OK, "카테고리 조회에 성공했습니다."),
GET_HISTORY_LIST_SUCCESS(HttpStatus.OK, "히스토리 목록 조회에 성공했습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ private Member getMember(OAuthAttributes attributes) {
* 처음 가입하는 회원이라면 Member 테이블을 생성합니다. (소셜 회원가입)
**/
private Member saveMember(OAuthAttributes attributes, String userId) {
//기존 유저와 아이디가 같으면 같은 유저임
// update는 기존 유저의 소셜 ID 컬럼에 값을 추가하는 것 정도만 있으면 될듯
Member member = memberRepository.getMemberByUserId(userId);
member.changeSocialId(attributes.getOAuth2UserInfo().getId());
return memberRepository.save(member);
}

//처음 가입
private Member saveMember(OAuthAttributes attributes) {
//기존 유저와 아이디가 같으면 같은 유저임
// update는 기존 유저의 소셜 ID 컬럼에 값을 추가하는 것 정도만 있으면 될듯
Member member = attributes.toEntity(attributes.getOAuth2UserInfo(), generateRandomId());
return memberRepository.save(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uspray.uspray.DTO.ApiResponseDto;
import com.uspray.uspray.DTO.auth.TokenDto;
import com.uspray.uspray.Enums.Authority;
import com.uspray.uspray.exception.SuccessStatus;
import com.uspray.uspray.jwt.TokenProvider;
import java.io.IOException;
Expand Down Expand Up @@ -32,6 +33,16 @@ public class OAuth2LoginSuccessHandler implements AuthenticationSuccessHandler {
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {

CustomOAuth2User customOAuth2User = (CustomOAuth2User) authentication.getPrincipal();

// User의 Role이 GUEST일 경우 처음 요청한 회원이므로 회원가입 페이지로 리다이렉트
if(customOAuth2User.getAuthority() == Authority.ROLE_GUEST) {
String accessToken = tokenProvider.generateTokenDto(authentication).getAccessToken();
response.addHeader("Authorization", "Bearer " + accessToken);
response.sendRedirect("/socialLoginNameInput"); // 프론트의 회원가입 추가 정보 입력 폼으로 리다이렉트 (추가 컨트롤러를 만들고 거기서 post 해야지 User로 바뀜)
return;
}

TokenDto tokenDto = tokenProvider.generateTokenDto(authentication);
redisTemplate.opsForValue().set("RT:" + authentication.getName(),
tokenDto.getRefreshToken(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Member toEntity(OAuth2UserInfo oAuth2UserInfo, String randomId) {
.name("Uspray")
.userId(randomId)
.socialId(oAuth2UserInfo.getId())
.authority(Authority.ROLE_USER)
.authority(Authority.ROLE_GUEST)
.build();
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/uspray/uspray/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.uspray.uspray.service;

import com.uspray.uspray.DTO.notification.NotificationAgreeDto;
import com.uspray.uspray.Enums.Authority;
import com.uspray.uspray.domain.Member;
import com.uspray.uspray.infrastructure.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -21,4 +23,11 @@ public void changePhone(String userId, String phone) {
public void changeNotificationAgree(String userId, NotificationAgreeDto notificationAgreeDto) {
memberRepository.getMemberByUserId(userId).changeNotificationSetting(notificationAgreeDto);
}

@Transactional
public void changeName(String userId, String name) {
Member member = memberRepository.getMemberByUserId(userId);
member.changeName(name);
member.changeAuthority(Authority.ROLE_USER);
}
}

0 comments on commit 2043bad

Please sign in to comment.