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

Hotfix/#123/로그인 성공시 userid return #124

Merged
merged 3 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -35,7 +35,7 @@ public class OAuthController {
* 유저가 카카오 로그인 동의 시 호출될 콜백 함수
*/
@GetMapping("/callback/kakao")
public BaseResponse<String> kakaoCallback(@RequestParam(name = "code") String code, HttpServletResponse response) {
public BaseResponse<Long> kakaoCallback(@RequestParam(name = "code") String code, HttpServletResponse response) {

// TODO 1. 인가코드 받기
// 카카오 인증 서버는 서비스 서버의 Redirect URI로 인가 코드를 전달함
Expand Down Expand Up @@ -70,6 +70,6 @@ public BaseResponse<String> kakaoCallback(@RequestParam(name = "code") String co
response.setHeader("Authorization", "Bearer " + jwtOAuthLogin);
log.info("jwtOAuthLogin = {}", jwtOAuthLogin);

return new BaseResponse<>("카카오 로그인 성공");
return new BaseResponse<>(userByOAuthInfo.getUserId());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3: 그럼 "result": 1 이런 형식으로 가는 건가요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! dto를 생성할까 했는데 그냥 바로 userId 값을 넘겨도 괜찮지 않을까 해서 이렇게 코드를 구성했습니다
혹시 response dto를 구성하는게 더 나을까요??

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정확히 어떤 값인지 알기 위해서 명시해주는 것도 좋다고 생각합니다.
"userId : 1 이런 식으로요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 내용 반영해서 response dto 생성해서 userId를 전달하는 방식으로 추가 커밋 하였습니다!

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.*;
import space.space_spring.dto.user.GetUserProfileListDto;
import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.user.PostLoginDto;
import space.space_spring.dto.user.request.PostUserLoginRequest;
import space.space_spring.dto.user.request.PostUserSignupRequest;
import space.space_spring.dto.user.response.GetSpaceInfoForUserResponse;
Expand Down Expand Up @@ -49,15 +50,15 @@ public BaseResponse<String> signup(@Validated @RequestBody PostUserSignupRequest
* 로그인
*/
@PostMapping("/login")
public BaseResponse<String> login(@Validated @RequestBody PostUserLoginRequest postUserLoginRequest, BindingResult bindingResult, HttpServletResponse response) {
public BaseResponse<PostLoginDto.Response> login(@Validated @RequestBody PostLoginDto.Request request, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
throw new CustomException(INVALID_USER_LOGIN, getErrorMessage(bindingResult));
}

String jwtLogin = userService.login(postUserLoginRequest);
response.setHeader("Authorization", "Bearer " + jwtLogin);
PostLoginDto login = userService.login(request);
response.setHeader("Authorization", "Bearer " + login.getJwt());

return new BaseResponse<>("로컬 로그인 성공");
return new BaseResponse<>(new PostLoginDto.Response(login.getUserId()));
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/space/space_spring/dto/user/PostLoginDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package space.space_spring.dto.user;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class PostLoginDto {

private String jwt;

private Long userId;

@Getter
@Setter
@NoArgsConstructor
public static class Request {
// '@', '.' 이 있어야 함
@Pattern(regexp = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$", message = "이메일 형식에 맞지 않습니다.")
@NotBlank
private String email;

@Pattern(
regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,16}$",
message = "8~16글자의 영문 대/소문자, 숫자, 특수문자가 포함되어야 합니다."
)
@NotBlank
private String password;
}

@Getter
@AllArgsConstructor
public static class Response {

private Long userId;
}
}
12 changes: 8 additions & 4 deletions src/main/java/space/space_spring/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.transaction.annotation.Transactional;
import space.space_spring.dao.UserSpaceDao;
import space.space_spring.dto.user.GetUserProfileListDto;
import space.space_spring.dto.user.PostLoginDto;
import space.space_spring.dto.user.dto.SpaceChoiceViewDto;
import space.space_spring.dto.user.request.PostUserLoginRequest;
import space.space_spring.dto.user.request.PostUserSignupRequest;
Expand Down Expand Up @@ -59,19 +60,22 @@ private void validateEmailForLocalSignup(String email) {
}

@Transactional
public String login(PostUserLoginRequest postUserLoginRequest) {
public PostLoginDto login(PostLoginDto.Request request) {
// TODO 1. 이메일 존재 여부 확인(아이디 존재 여부 확인)
User userByEmail = userUtils.findUserByEmail(postUserLoginRequest.getEmail(), LOCAL);
User userByEmail = userUtils.findUserByEmail(request.getEmail(), LOCAL);
log.info("userByEmail.getUserId: {}", userByEmail.getUserId());

// TODO 2. 비밀번호 일치 여부 확인
validatePassword(userByEmail, postUserLoginRequest.getPassword());
validatePassword(userByEmail, request.getPassword());

// TODO 3. JWT 발급
String jwtLogin = jwtLoginProvider.generateToken(userByEmail);
log.info("jwtLogin: {}", jwtLogin);

return jwtLogin;
return new PostLoginDto(
jwtLogin,
userByEmail.getUserId()
);
}

private void validatePassword(User userByEmail, String password) {
Expand Down
Loading