Skip to content

Commit

Permalink
refactor : memberService로 request 객체 -> String, Long으로 바꾸기
Browse files Browse the repository at this point in the history
  • Loading branch information
chwangmin committed May 19, 2024
1 parent ad66b87 commit 8fccfcb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class MemberController {
@PostMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> register(@RequestBody RegisterRequest registerRequest){

memberService.register(registerRequest);
memberService.register(registerRequest.getEmail(), registerRequest.getPassword(), registerRequest.getName());

return ResponseEntity.status(HttpStatus.OK).body("signup success");
}
Expand All @@ -51,7 +51,7 @@ public ResponseEntity<String> register(@RequestBody RegisterRequest registerRequ
@PostMapping("/login")
public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest, HttpServletResponse response){

TokenResponse tokenResponse = memberService.login(loginRequest);
TokenResponse tokenResponse = memberService.login(loginRequest.getEmail(), loginRequest.getPassword());

response.addHeader(JwtTokenProvider.AUTHORIZATION_HEADER, tokenResponse.getAccessToken());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.ssafy.home.domain.member.service;

import com.ssafy.home.domain.member.dto.request.LoginRequest;
import com.ssafy.home.domain.member.dto.request.RegisterRequest;
import com.ssafy.home.domain.member.dto.response.TokenResponse;
import com.ssafy.home.domain.member.repository.GeneralMemberRepository;
import com.ssafy.home.domain.member.repository.LoginAttemptRepository;
Expand Down Expand Up @@ -44,32 +42,33 @@ public class MemberService {
private final Encryption encryption;

@Transactional
public void register(RegisterRequest registerRequest) {
public void register(String email, String password, String name) {

if (memberRepository.existsByEmail(registerRequest.getEmail())) {
if (memberRepository.existsByEmail(email)) {
throw new AuthenticationException(ErrorCode.MEMBER_NOT_MATCH);
}

String salt = encryption.getSalt();
String password = "";

try {
password = encryption.Hashing(registerRequest.getPassword().getBytes(), salt);
password = encryption.Hashing(password.getBytes(), salt);
} catch (Exception e) {
throw new BusinessException(ErrorCode.INVALID_AES_KEY);
}

Member member = memberRepository.save(registerRequest.toEntity());
Member member = memberRepository.save(Member.builder()
.name(name)
.email(email)
.build());
GeneralMember generalMember = generalMemberRepository.save(GeneralMember.builder().member(member).userEncPassword(password).build());
memberSecurityRepository.save(MemberSecret.builder().generalMember(generalMember).salt(salt).build());
loginAttemptRepository.save(LoginAttempt.builder().member(member).build());

}

@Transactional
public TokenResponse login(LoginRequest loginRequest) {
public TokenResponse login(String email, String password) {

Member member = memberRepository.findByEmail(loginRequest.getEmail())
Member member = memberRepository.findByEmail(email)
.orElseThrow(() -> new AuthenticationException(ErrorCode.MEMBER_NOT_MATCH));

if (member.getLoginAttempt().getCount() >= 5) {
Expand All @@ -80,7 +79,7 @@ public TokenResponse login(LoginRequest loginRequest) {

String salt = member.getGeneralMember().getMemberSecret().getSalt();

String encPassword = encryption.Hashing(loginRequest.getPassword().getBytes(), salt);
String encPassword = encryption.Hashing(password.getBytes(), salt);

if (!member.getGeneralMember().getUserEncPassword().equals(encPassword)) {
throw new AuthenticationException(ErrorCode.MEMBER_NOT_MATCH);
Expand Down

0 comments on commit 8fccfcb

Please sign in to comment.