Skip to content

Commit

Permalink
[FEAT]#13 로그인 인증과정 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 committed Aug 27, 2023
1 parent 85ed714 commit 4a6a1f2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

import com.example.rcp1.domain.user.domain.User;
import com.example.rcp1.domain.user.domain.repository.UserRepository;
import com.example.rcp1.domain.user.dto.SignInReq;
import com.example.rcp1.domain.user.dto.SignUpReq;
import com.example.rcp1.global.BaseResponse;
import com.example.rcp1.global.SuccessCode;
import com.example.rcp1.global.CustomAuthenticationException;
import com.example.rcp1.global.config.security.util.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -57,9 +50,30 @@ public User signUp(SignUpReq signUpReq) {
}


public String login(String email, String password) {
public String signIn(SignInReq signInReq) {
// 인증 과정
return JwtUtil.createJwt(email, secret_key, expiredMs);
// Optional<User> byEmail = userRepository.findByEmail(signInReq.getEmail());
// System.out.println("byEmail = " + byEmail);
// if (!BCrypt.checkpw(password, user.getPassword())) {
//
// return null;
// }

// 이메일을 통해 사용자 정보 조회
Optional<User> byEmail = userRepository.findByEmail(signInReq.getEmail());

if (byEmail.isPresent()) {
User user = byEmail.get();
if (BCrypt.checkpw(signInReq.getPassword(), user.getPassword())) {
return JwtUtil.createJwt(signInReq.getEmail(), secret_key, expiredMs);
} else {
throw new CustomAuthenticationException("비밀번호가 일치하지 않습니다.");
}
}

// return JwtUtil.createJwt(signInReq.getEmail(), secret_key, expiredMs);

throw new CustomAuthenticationException("사용자를 찾을 수 없습니다.");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.rcp1.domain.user.domain.repository;

import com.example.rcp1.domain.user.domain.User;
import com.example.rcp1.domain.user.dto.SignInReq;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.security.core.userdetails.UserDetails;

Expand All @@ -9,4 +10,5 @@
public interface UserRepository extends JpaRepository<User, Long> {


Optional<User> findByEmail(String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.rcp1.domain.user.dto.SignInReq;
import com.example.rcp1.domain.user.dto.SignUpReq;
import com.example.rcp1.global.BaseResponse;
import com.example.rcp1.global.CustomAuthenticationException;
import com.example.rcp1.global.ErrorCode;
import com.example.rcp1.global.SuccessCode;
import io.swagger.models.Response;
Expand Down Expand Up @@ -40,12 +41,18 @@ public ResponseEntity<BaseResponse<User>> signUp(@Valid @RequestBody SignUpReq s
@PostMapping("/signIn")
public ResponseEntity<BaseResponse<String>> signIn(@Valid @RequestBody SignInReq signInReq) {
try {
String token = userService.login(signInReq.getEmail(), signInReq.getPassword());
return ResponseEntity.ok(BaseResponse.success(SuccessCode.SIGNIN_SUCCESS, token));
} catch (Exception e) {
String token = userService.signIn(signInReq);
if (token != null) {
return ResponseEntity.ok(BaseResponse.success(SuccessCode.SIGNIN_SUCCESS, token));
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(BaseResponse.error(ErrorCode.EXPIRED_TOKEN, "로그인에 실패했습니다."));
}
} catch (CustomAuthenticationException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(BaseResponse.error(ErrorCode.REQUEST_VALIDATION_EXCEPTION, "로그인에 실패했습니다."));
.body(BaseResponse.error(ErrorCode.REQUEST_VALIDATION_EXCEPTION, e.getMessage()));
}

// return ResponseEntity.ok(BaseResponse.success(SuccessCode.SIGNIN_SUCCESS, userService.login("이재혁", "")));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.rcp1.global;

import lombok.RequiredArgsConstructor;

public class CustomAuthenticationException extends RuntimeException {
public CustomAuthenticationException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

// 토큰 안보내면 블락
if (authorization == null || !authorization.startsWith("Bearer ")) {
log.error("authentication을 잘못 보냈습니다.");
// log.error("authentication을 잘못 보냈습니다.");
filterChain.doFilter(request, response);
return;
}
Expand Down

0 comments on commit 4a6a1f2

Please sign in to comment.