Skip to content
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
Expand Up @@ -14,10 +14,12 @@ public enum ErrorCode implements BaseCode {

// User
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "USER_4041", "존재하지 않는 회원입니다."),
USER_NOT_FOUND_BY_EMAIL(HttpStatus.NOT_FOUND, "USER_4042", "존재하지 않는 회원입니다.-EMAIL"),
USER_NOT_FOUND_BY_USERNAME(HttpStatus.NOT_FOUND, "USER_4043", "존재하지 않는 회원입니다.-USERNAME & KEY"),
USER_NOT_FOUND_BY_EMAIL(HttpStatus.NOT_FOUND, "USER_4042", "해당 email을 가진 회원이 없습니다."),
USER_NOT_FOUND_BY_USERNAME(HttpStatus.NOT_FOUND, "USER_4043", "해당 key를 가진 회원이 없습니다."),
USER_NOT_OLDER(HttpStatus.FORBIDDEN, "USER_4031", "해당 유저가 노인이 아니랍니다."),
USER_NOT_GUARDIAN(HttpStatus.FORBIDDEN, "USER_4032", "해당 유저가 보호자가 아닙니다."),
USER_ROLE_DIFFERENT(HttpStatus.FORBIDDEN, "USER_4033", "해당 유저의 role이 틀립니다."),
USER_ALREADY_MEMBER(HttpStatus.CONFLICT, "USER_4091", "이미 회원가입된 유저입니다."),

// Jwt
WRONG_REFRESH_TOKEN(HttpStatus.NOT_FOUND, "JWT_4041", "일치하는 리프레시 토큰이 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public enum SuccessCode implements BaseCode {
CREATED(HttpStatus.CREATED, "COMMON_201", "Created"),

// User
USER_SOCIAL_LOGIN_SUCCESS(HttpStatus.CREATED, "USER_2011", "소셜 회원가입 혹은 로그인이 완료되었습니다."),
USER_KEY_LOGIN_SUCCESS(HttpStatus.CREATED, "USER_2012", "KEY 로그인이 완료되었습니다."),
USER_SOCIAL_SIGNIN_SUCCESS(HttpStatus.CREATED, "USER_2011", "소셜 회원가입이 완료되었습니다."),
USER_SOCIAL_LOGIN_SUCCESS(HttpStatus.CREATED, "USER_2012", "소셜 로그인이 완료되었습니다."),
USER_KEY_LOGIN_SUCCESS(HttpStatus.CREATED, "USER_2013", "KEY 로그인이 완료되었습니다."),
USER_LOGOUT_SUCCESS(HttpStatus.OK, "USER_2001", "로그아웃 되었습니다."),
USER_REISSUE_SUCCESS(HttpStatus.OK, "USER_2002", "토큰 재발급이 완료되었습니다."),
USER_DELETE_SUCCESS(HttpStatus.OK, "USER_2003", "회원탈퇴가 완료되었습니다."),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,64 @@
public class UserController {
private final UserService userService;

@Operation(summary = "소셜 회원가입", description = "프론트에게 유저 정보 받아 소셜 회원가입 후, 토큰 반환하는 메서드입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER_2011", description = "소셜 회원가입 성공"),
})
@PostMapping("/social/sign-in")
public ApiResponse<JwtDto> socialSignIn(
@RequestBody UserRequestDto.UserSigInReqDto userReqDto
) throws Exception {
userService.validateNewUser(userReqDto.getEmail());

User user = userService.createUser(userReqDto);

JwtDto jwt = userService.jwtMakeSave(user.getUsername());
String accessToken = jwt.getAccessToken();
String refreshToken = jwt.getRefreshToken();

return ApiResponse.onSuccess(SuccessCode.USER_SOCIAL_SIGNIN_SUCCESS,
UserConverter.jwtDto(accessToken, refreshToken, user.getUsername()));
}

@Operation(summary = "소셜 로그인", description = "프론트에게 유저 정보 받아 소셜 회원가입 후, 토큰 반환하는 메서드입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER_2012", description = "소셜 로그인 성공"),
})
@PostMapping("/social/login")
public ApiResponse<JwtDto> socialLogin(
@RequestBody UserRequestDto.UserLoginReqDto userReqDto
) {
userService.validateOldUser(userReqDto);

User user = userService.findByEmail(userReqDto.getEmail());

JwtDto jwt = userService.jwtMakeSave(user.getUsername());
String accessToken = jwt.getAccessToken();
String refreshToken = jwt.getRefreshToken();

return ApiResponse.onSuccess(SuccessCode.USER_SOCIAL_LOGIN_SUCCESS,
UserConverter.jwtDto(accessToken, refreshToken, user.getUsername()));
}

@Operation(summary = "key 로그인", description = "노인의 key 로그인 후, 프론트에게 유저 정보 받아 토큰 반환하는 메서드입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER_2013", description = "회원가입 & 로그인 성공"),
})
@PostMapping("/key/login")
public ApiResponse<JwtDto> keyLogin(
@RequestParam String key
) {
User user = userService.findByUserName(key);

JwtDto jwt = userService.jwtMakeSave(user.getUsername());
String accessToken = jwt.getAccessToken();
String refreshToken = jwt.getRefreshToken();

return ApiResponse.onSuccess(SuccessCode.USER_KEY_LOGIN_SUCCESS,
UserConverter.jwtDto(accessToken, refreshToken, "wasUser"));
}

@Operation(summary = "로그아웃", description = "로그아웃하는 메서드입니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER_2001", description = "로그아웃 되었습니다."),
Expand Down Expand Up @@ -146,7 +204,7 @@ public ApiResponse<Boolean> assignOlder(
@PostMapping(value = "/guardians/olders")
public ApiResponse<String> registerOlder(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@RequestBody UserRequestDto.UserReqDto userReqDto
@RequestBody UserRequestDto.UserSigInReqDto userReqDto
) throws Exception {
User guardian = userService.findByUserName(customUserDetails.getUsername());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;

public class UserConverter {
public static User saveUser(UserRequestDto.UserReqDto userReqDto, String key) {
public static User saveUser(UserRequestDto.UserSigInReqDto userReqDto, String key) {

return User.builder()
.role(userReqDto.getRole())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

@NoArgsConstructor
public class UserRequestDto {
@Schema(description = "UserReqDto")
@Schema(description = "UserSigInReqDto")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class UserReqDto {
public static class UserSigInReqDto {
@Schema(description = "역할(OLDER/GUARDIAN)")
private UserRole role;

Expand All @@ -31,6 +31,19 @@ public static class UserReqDto {

}

@Schema(description = "UserLoginReqDto")
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class UserLoginReqDto {
@Schema(description = "역할(OLDER/GUARDIAN)")
private UserRole role;

@Schema(description = "이메일")
private String email;
}

@Schema(description = "UserNicknameReqDto")
@Getter
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.example.silverbridgeX_user.user.dto.JwtDto;
import com.example.silverbridgeX_user.user.dto.UserRequestDto;
import com.example.silverbridgeX_user.user.dto.UserRequestDto.UserPreferenceDto;
import com.example.silverbridgeX_user.user.dto.UserRequestDto.UserReqDto;
import com.example.silverbridgeX_user.user.dto.UserRequestDto.UserSigInReqDto;
import com.example.silverbridgeX_user.user.jwt.JwtTokenUtils;
import com.example.silverbridgeX_user.user.repository.RefreshTokenRepository;
import com.example.silverbridgeX_user.user.repository.UserRepository;
Expand Down Expand Up @@ -72,7 +72,24 @@ public Boolean checkMemberByEmail(String email) {
}

@Transactional
public User createUser(UserReqDto userReqDto) throws Exception {
public void validateNewUser(String email) {
if (userRepository.existsByEmail(email)) {
throw new GeneralException(ErrorCode.USER_ALREADY_MEMBER);
}
}

@Transactional
public void validateOldUser(UserRequestDto.UserLoginReqDto userReqDto) {
User older = userRepository.findByEmail(userReqDto.getEmail())
.orElseThrow(() -> new GeneralException(ErrorCode.USER_NOT_FOUND_BY_EMAIL));

if (userReqDto.getRole() != older.getRole()) {
throw new GeneralException(ErrorCode.USER_ROLE_DIFFERENT);
}
}

@Transactional
public User createUser(UserSigInReqDto userReqDto) throws Exception {

String uniqueKey;
do {
Expand Down