Skip to content

Commit

Permalink
[FEAT] add guardian in User
Browse files Browse the repository at this point in the history
  • Loading branch information
devyubin committed Aug 13, 2023
1 parent c6faa34 commit c509ef7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class AuthController {
private long CookiePeriod;

@PostMapping("/kakao")
public ResponseEntity<BaseResponse> loginKakao(@RequestBody KakaoLoginParams params) {
public ResponseEntity<BaseResponse> loginKakao(@RequestParam Boolean isGuardian, @RequestBody KakaoLoginParams params) {
try {
RespLoginDto respLoginDto = authService.login(params);
log.debug("isGuardian = {}", isGuardian);
RespLoginDto respLoginDto = authService.login(params, isGuardian);

HttpHeaders headers = respLoginDto.getHeaders();
LoginDto loginDto = respLoginDto.getLoginDto();
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/haemil/backend/auth/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public class AuthService {
private long CookiePeriod;

// 로그인: 인증 정보 저장 및 비어 토큰 발급
public RespLoginDto login(OAuthLoginParams params) throws BaseException {
public RespLoginDto login(OAuthLoginParams params, Boolean isGuardian) throws BaseException {

OAuthInfoResponse oAuthInfoResponse = requestOAuthInfoService.request(params);
Long userId = findOrCreateUser(oAuthInfoResponse);
Long userId = findOrCreateUser(oAuthInfoResponse, isGuardian);
AuthTokens token = createToken(userId, oAuthInfoResponse);

User user = userRepository.findById(userId).orElseThrow(() -> new BaseException(ResponseStatus.NO_USER));
Expand Down Expand Up @@ -218,19 +218,20 @@ public ResponseCookie saveHttpCookie(AuthTokens token) {
return httpCookie;
}

private Long findOrCreateUser(OAuthInfoResponse oAuthInfoResponse) {
private Long findOrCreateUser(OAuthInfoResponse oAuthInfoResponse, Boolean isGuardian) {
return userRepository.findByEmail(oAuthInfoResponse.getEmail())
.map(User::getId)
.orElseGet(() -> newUser(oAuthInfoResponse));
.orElseGet(() -> newUser(oAuthInfoResponse, isGuardian));
}

private Long newUser(OAuthInfoResponse oAuthInfoResponse) {
private Long newUser(OAuthInfoResponse oAuthInfoResponse, Boolean isGuardian) {
User user = User.builder()
.email(oAuthInfoResponse.getEmail())
.nickname(oAuthInfoResponse.getNickname())
.profileImageUrl(oAuthInfoResponse.getProfileImageUrl())
.oAuthProvider(oAuthInfoResponse.getOAuthProvider())
.role(User.Role.USER) // 추가.
.guardian(isGuardian)
.build();

return userRepository.save(user).getId();
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/haemil/backend/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,30 @@ public class User {
// 프로필 이미지 url 추가.
@Column
private String profileImageUrl;

private OAuthProvider oAuthProvider;

@Getter
@RequiredArgsConstructor
public enum Role {
USER("ROLE_USER"), ADMIN("ROLE_ADMIN");

private final String key;
}

// 보호자 여부
public Boolean guardian;

// public enum OAuthProvider {
// KAKAO, GOOGLE
// }

@Builder
public User(String email, String nickname, String profileImageUrl, Role role, OAuthProvider oAuthProvider) {
public User(String email, String nickname, String profileImageUrl, Role role, OAuthProvider oAuthProvider, Boolean guardian) {
this.email = email;
this.nickname = nickname;
this.profileImageUrl = profileImageUrl;
this.role = role;
this.oAuthProvider = oAuthProvider;
this.guardian = guardian;
}

public User update(String name, String picture){
Expand Down

0 comments on commit c509ef7

Please sign in to comment.