- TODO API 명세서 기준으로 수정로직 확인하기
- 시큐리티 관련 부분들 제외
- 로그인 관련 로직 제외
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AccountController {
private final AccountService accountService;
@PostMapping("/signup")
public ResponseEntity<?> registerAccount(@RequestBody AccountJoinRequestDto requestDto) {
accountService.registerNewAccount(requestDto);
return ResponseEntity.ok().build();
}
}
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AccountJoinRequestDto {
@NotBlank(message = "이메일은 비어있을 수 없습니다")
@Email(message = "형식에 맞게 입력하세요")
private String email;
@NotBlank(message = "비밀번호는 비어있을 수 없습니다")
@Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]{8,15}$",
message = "비밀번호는 8~15자리면서 알파벳, 숫자, 특수문자를 포함해야합니다")
private String password;
@NotBlank(message = "주소는 비어있을 수 없습니다")
private String address;
@NotBlank(message = "전화번호는 비어있을 수 없습니다")
@Pattern(regexp = "^[0-9]{10,11}$", message = "전화번호는 10~11자리의 숫자이어야 합니다")
private String phone;
@Builder
public Account toEntity() {
return Account.builder()
.email(email)
.password(password)
.phone(phone)
.address(address)
.build();
}
}
- Entity 클래스에 'isOwner' 추가 해야함
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class AccountLoginRequestDto {
@Email(message = "형식에 맞게 입력하세요")
@NotBlank(message = "이메일은 비어있을 수 없습니다")
private String email;
@NotBlank(message = "비밀번호는 비어있을 수 없습니다")
@Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]{8,15}$",
message = "비밀번호는 8~15자리면서 알파벳, 숫자, 특수만자를 포함해야합니다")
private String password;
}
@Getter
@AllArgsConstructor
public class AccountLoginResponseDto {
private Long id; // 추가 부분
private RoleTypeEnum role;
}
- 'Id' 필드가 없었지만 요청으로 인한 추가
- jwt 토큰을 쿠키에 담아서 반환 요청 -> jwt 관련 로직 확인하기
@Entity
@Getter
@NoArgsConstructor
public class Account {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String email;
private String password;
private String phone;
private String address;
private Long point;
@Enumerated(EnumType.STRING)
private RoleTypeEnum role;
@OneToOne(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
private Store store;
@Builder
public Account(String email,
String password,
String phone,
String address,
RoleTypeEnum role) {
this.email = email;
this.password = password;
this.phone = phone;
this.address = address;
this.role = role;
this.point = role == RoleTypeEnum.CUSTOMER ? 1000000L : 0L;
}
}
- 'role' 삭제해야 할 듯
- 'isOwner' 추가 해아 함 -> 기본값은 고객으로 주고 시작하는가?
@RequiredArgsConstructor
public enum RoleTypeEnum {
CUSTOMER(Authority.CUSTOMER),
OWNER(Authority.OWNER);
private final String authority;
public String getAuthority() {
return this.authority;
}
public static class Authority {
public static final String CUSTOMER = "ROLE_CUSTOMER";
public static final String OWNER = "ROLE_OWNER";
}
}
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
Optional<Account> findByEmail(String email);
boolean existsByEmail(String email);
}
@Service
@RequiredArgsConstructor
public class AccountService {
private final AccountRepository accountRepository;
private final PasswordEncoder passwordEncoder;
@Transactional
public void registerNewAccount(AccountJoinRequestDto requestDto) {
// 이메일 중복 확인
String email = requestDto.getEmail();
if (accountRepository.existsByEmail(requestDto.getEmail())) {
throw new IllegalStateException("이미 존재하는 이메일입니다.");
}
// 비밀번호 인코딩
String password = passwordEncoder.encode(requestDto.getPassword());
Account account = Account.builder()
.email(email)
.password(password)
.phone(requestDto.getPhone())
.address(requestDto.getAddress())
.build();
// 계정 저장
accountRepository.save(account);
}
}
- 'role' 부분 삭제
'기록하기' 카테고리의 다른 글
Docker (0) | 2023.11.21 |
---|---|
Day + 37 (0) | 2023.11.21 |
Day + 35 (0) | 2023.11.19 |
챌린지 프로젝트 시작 (1) | 2023.11.17 |
Docker 삭제 명령어 (0) | 2023.11.16 |