feat: user중복로그인 체크

This commit is contained in:
yulmam 2024-08-01 10:26:47 +09:00
parent 56970789d2
commit 555df4106c
3 changed files with 17 additions and 33 deletions

View File

@ -33,29 +33,22 @@ public class UserController {
@PostMapping("/join")
public ResponseEntity<String> join(@RequestBody User user) throws Exception {
System.out.println("@@@@@");
log.info("@@@@@@@@@@@@@@@@");
userService.join(user);
return ResponseEntity.ok("User registered successfully");
}
@PostMapping("/findpassword/{user_id}")
public ResponseEntity<String> findpassword(@PathVariable("user_id") Long user_id) throws Exception {
userService.userCheck(user_id);
return ResponseEntity.ok("임시 비밀번호가 이메일로 전송되었습니다.");
}
@PutMapping("/updateinfo")
public ResponseEntity<String> updateUserInfo(
@RequestBody InfoDto infoDto, HttpServletRequest request) {
try {
String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token));
userService.changeuInfo(infoDto, userId);
userService.changeUserInfo(infoDto, userId);
return ResponseEntity.ok("User info updated successfully");
} catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
@ -80,36 +73,35 @@ public class UserController {
@Operation(summary = "로그인", description = "아이디와 비밀번호를 이용하여 로그인 처리.")
@PostMapping("/login")
public ResponseEntity<Map<String, Object>> login(
@RequestBody @Parameter(description = "로그인 시 필요한 회원정보(아이디, 비밀번호).", required = true) User user, HttpServletResponse response) {
@RequestBody @Parameter(description = "로그인 시 필요한 회원정보(아이디, 비밀번호).", required = true) User user, HttpServletRequest request, HttpServletResponse response) {
String token = request.getHeader("Authorization");
if(jwtUtil.checkToken(token)){
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
Map<String, Object> resultMap = new HashMap<>();
HttpStatus status = HttpStatus.ACCEPTED;
try {
User loginUser = userService.login(user);
if (loginUser != null) {
String name = loginUser.getName();
resultMap.put("name",name);
String accessToken = jwtUtil.createAccessToken(String.valueOf(loginUser.getId()));
String refreshToken = jwtUtil.createRefreshToken(String.valueOf(loginUser.getId()));
userService.saveRefreshToken(loginUser.getId(), refreshToken);
resultMap.put("name",loginUser.getName());
resultMap.put("role",loginUser.getRole());
resultMap.put("access-token", accessToken);
Cookie refreshCookie = new Cookie("refresh-token", refreshToken);
refreshCookie.setPath("/");
refreshCookie.setHttpOnly(true);
refreshCookie.setSecure(true); // HTTPS에서만 전송되도록 설정
//r/efreshCookie.setSameSite(Cookie.SameSite.NONE); // Cross-Origin 요청에 대해 모두 전송
//refreshCookie.setSameSite(Cookie.SameSite.NONE); // Cross-Origin 요청에 대해 모두 전송
//refreshCookie.setSameSite("None"); // Cross-Origin 요청에 대해 모두 전송
String cookieHeader = String.format("refresh-token=%s; Path=/; HttpOnly; Secure; SameSite=None", refreshToken);
@ -118,7 +110,6 @@ public class UserController {
// refreshCookie.setSameSite("None"); // Cross-Origin 요청에 대해 모두 전송
response.addCookie(refreshCookie);
status = HttpStatus.CREATED;
} else {
resultMap.put("message", "아이디 또는 패스워드를 확인해 주세요.");

View File

@ -14,7 +14,7 @@ public interface UserService {
void sendEamail(User user) throws Exception;
void userCheck(Long id) throws Exception;
String getUserName(Long id) throws Exception;
void changeuInfo(InfoDto infoDto,Long id) throws Exception;
void changeUserInfo(InfoDto infoDto,Long id) throws Exception;
void changePassword(PasswordDto passwordDto,Long id) throws Exception;
}

View File

@ -119,23 +119,16 @@ public class UserServiceImpl implements UserService {
@Override
public void changeuInfo(InfoDto infoDto, Long id) throws Exception {
public void changeUserInfo(InfoDto infoDto, Long id) throws Exception {
User user = userRepository.findById(id).orElse(null);
if (user == null) {
throw new Exception("User not found");
}
User user = userRepository.findById(id).orElseThrow(IllegalArgumentException::new);
if (infoDto.getName() != null)
{
user.setName(infoDto.getName());
}
if(infoDto.getEmail()!=null)
{
user.setEmail(infoDto.getEmail());
}
userRepository.save(user);
}