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

πŸ”€ :: νšŒμ›κ°€μž…μ‹œμ— 학생과 μ„ μƒλ‹˜ κ΅¬λ³„ν•˜μ—¬ κΆŒν•œ λΆ€μ—¬ν•˜κΈ° #51

Merged
merged 6 commits into from
Apr 8, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mindway.server.v2.global.exception.ErrorCode;
import com.mindway.server.v2.global.exception.MindWayException;

public class MemberNotFoundException extends MindWayException {
public class UserNotFoundException extends MindWayException {

public MemberNotFoundException() {
public UserNotFoundException() {
super(ErrorCode.MEMBER_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mindway.server.v2.domain.auth.RefreshToken;
import com.mindway.server.v2.domain.auth.exception.ExpiredRefreshTokenException;
import com.mindway.server.v2.domain.auth.exception.MemberNotFoundException;
import com.mindway.server.v2.domain.auth.exception.UserNotFoundException;
import com.mindway.server.v2.domain.auth.presentation.dto.response.TokenResponse;
import com.mindway.server.v2.domain.auth.repository.RefreshRepository;
import com.mindway.server.v2.domain.auth.service.ReissueTokenService;
Expand All @@ -27,7 +27,7 @@ public TokenResponse execute(String refreshToken) {
.orElseThrow(ExpiredRefreshTokenException::new);

User user = userRepository.findById(refreshEntity.getMemberId())
.orElseThrow(MemberNotFoundException::new);
.orElseThrow(UserNotFoundException::new);

refreshRepository.deleteById(refreshEntity.getRefreshToken());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mindway.server.v2.domain.auth.service.impl;

import com.mindway.server.v2.domain.auth.RefreshToken;
import com.mindway.server.v2.domain.auth.exception.UserNotFoundException;
import com.mindway.server.v2.domain.auth.presentation.dto.request.SignInRequest;
import com.mindway.server.v2.domain.auth.presentation.dto.response.TokenResponse;
import com.mindway.server.v2.domain.auth.repository.RefreshRepository;
Expand All @@ -20,6 +21,7 @@
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@RequiredArgsConstructor
Expand Down Expand Up @@ -52,6 +54,9 @@ public TokenResponse execute(SignInRequest signInRequest) throws GAuthException{
User user = userRepository.findByEmail(userInfo.getEmail())
.orElseGet(() -> saveUser(userInfo));

if (user == null)
throw new UserNotFoundException();

TokenResponse tokenResponse = jwtProvider.generateTokenDto(user.getId());

saveRefreshToken(tokenResponse, user);
Expand All @@ -68,12 +73,20 @@ public TokenResponse execute(SignInRequest signInRequest) throws GAuthException{
}

private User saveUser(GAuthUserInfo gAuthUserInfo) {
if (Objects.equals(gAuthUserInfo.getRole(), "ROLE_STUDENT")) {
return saveStudent(gAuthUserInfo);
} else if (Objects.equals(gAuthUserInfo.getRole(), "ROLE_TEACHER")) {
return saveTeacher(gAuthUserInfo);
}
return null;
}

private User saveStudent(GAuthUserInfo gAuthUserInfo) {
User user = User.builder()
.id(UUID.randomUUID())
.email(gAuthUserInfo.getEmail())
.name(gAuthUserInfo.getName())
.studentNum(new StudentNum(gAuthUserInfo.getGrade(), gAuthUserInfo.getClassNum(), gAuthUserInfo.getNum()))
.gauth_role(gAuthUserInfo.getRole())
.authority(Authority.ROLE_STUDENT)
.build();

Expand All @@ -82,6 +95,20 @@ private User saveUser(GAuthUserInfo gAuthUserInfo) {
return user;
}

private User saveTeacher(GAuthUserInfo gAuthUserInfo) {
User teacher = User.builder()
.id(UUID.randomUUID())
.email(gAuthUserInfo.getEmail())
.name(gAuthUserInfo.getName())
.studentNum(new StudentNum(gAuthUserInfo.getGrade(), gAuthUserInfo.getClassNum(), gAuthUserInfo.getNum()))
.authority(Authority.ROLE_TEACHER)
.build();

userRepository.save(teacher);

return teacher;
}

private void saveRefreshToken(TokenResponse tokenResponse, User user) {
RefreshToken refreshToken = RefreshToken.builder()
.refreshToken(tokenResponse.getRefreshToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class User {
@Embedded
private StudentNum studentNum;

private String gauth_role;

@Enumerated(EnumType.STRING)
private Authority authority;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mindway.server.v2.domain.user.util;

import com.mindway.server.v2.domain.auth.exception.MemberNotFoundException;
import com.mindway.server.v2.domain.auth.exception.UserNotFoundException;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,6 +16,6 @@ public class UserUtil {
public User getCurrentUser() {
String email = SecurityContextHolder.getContext().getAuthentication().getName();
return userRepository.findByEmail(email)
.orElseThrow(MemberNotFoundException::new);
.orElseThrow(UserNotFoundException::new);
}
}
Loading