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

[FEATURE] 회원가입 API 구현 #23

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -1,10 +1,12 @@
package HookKiller.server.auth.controller;

import HookKiller.server.auth.dto.request.AuthRequest;
import HookKiller.server.auth.dto.request.RegisterRequest;
import HookKiller.server.auth.dto.response.AuthResponse;
import HookKiller.server.auth.service.AuthService;
import HookKiller.server.auth.service.CustomUserDetailsService;
import HookKiller.server.jwt.JwtTokenProvider;
import HookKiller.server.user.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -22,10 +24,21 @@ public class AuthController {
private final JwtTokenProvider jwtTokenProvider;
private final CustomUserDetailsService customUserDetailsService;
private final AuthService authService;
private final UserService userService;

@PostMapping("/register")
public ResponseEntity<AuthResponse> register(@RequestBody @Valid RegisterRequest request) {
return userService.registerUser(request);
}

@PostMapping("/login")
public ResponseEntity<AuthResponse> loginExecute(@RequestBody @Valid AuthRequest body) {
return authService.login(body);
public ResponseEntity<AuthResponse> login(@RequestBody @Valid AuthRequest body) {
return authService.loginExecute(body);
}

@PostMapping("/logout")
public void logout(){
// React Cookie 삭제로 구현할 것. 따로 로직은 필요없음
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package HookKiller.server.auth.dto.request;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class RegisterRequest {

@NotEmpty(message = "이메일 입력은 필수 입니다.")
@Pattern(regexp = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,6}$", message = "이메일 형식에 맞지 않습니다.")
private String email;

@NotEmpty(message = "닉네임 입력은 필수 입니다.")
private String nickName;

@NotEmpty(message = "패스워드 입력은 필수 입니다.")
@Size(min = 3, max = 15)
// @Pattern(등등 추가)
private String password;

private String role;
}
11 changes: 8 additions & 3 deletions src/main/java/HookKiller/server/auth/service/AuthService.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package HookKiller.server.auth.service;

import HookKiller.server.auth.dto.request.AuthRequest;
import HookKiller.server.auth.dto.request.RegisterRequest;
import HookKiller.server.auth.dto.response.AuthResponse;
import HookKiller.server.auth.exception.UserNotFoundException;
import HookKiller.server.jwt.JwtTokenProvider;
import HookKiller.server.user.entity.User;
import HookKiller.server.user.exception.AlreadyExistUserException;
import HookKiller.server.user.repository.UserRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -21,8 +24,10 @@ public class AuthService {
private final JwtTokenProvider jwtTokenProvider;
private final UserRepository userRepository;

public ResponseEntity<AuthResponse> login(AuthRequest body) {
User user = userRepository.findByEmailAndPassword(body.getEmail(), body.getPassword()).orElseThrow(()-> UserNotFoundException.EXCEPTION );
private static final String BEARER = "Bearer ";

public ResponseEntity<AuthResponse> loginExecute(AuthRequest request) {
User user = userRepository.findByEmailAndPassword(request.getEmail(), request.getPassword()).orElseThrow(()-> UserNotFoundException.EXCEPTION );
AuthResponse res = AuthResponse.builder()
.token(jwtTokenProvider.generateToken(user.getId(), user.getEmail(),user.getNickName(), user.getRole()))
.build();
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/HookKiller/server/user/dto/request/UserLoginDto.java

This file was deleted.

14 changes: 12 additions & 2 deletions src/main/java/HookKiller/server/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;


@Entity
Expand All @@ -33,8 +35,16 @@ public class User extends AbstractTimeStamp {

@Enumerated(EnumType.STRING)
private UserRole role;

// private Long certificateKeyId;

@Builder
public User(String email, String password, String nickName, String role) {
this.email = email;
this.password = password;
this.nickName = nickName;
this.role = UserRole.valueOf(role);
}

// private Long certificateKeyId;

// private OauthInfo oauthInfo;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package HookKiller.server.user.exception;

import HookKiller.server.common.exception.BaseException;

public class AlreadyExistUserException extends BaseException {

public static final BaseException EXCEPTION = new AlreadyExistUserException();

private AlreadyExistUserException() {
super(UserException.ALREADY_EXIST_USER_ERROR);
}
}
27 changes: 27 additions & 0 deletions src/main/java/HookKiller/server/user/exception/UserException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package HookKiller.server.user.exception;

import HookKiller.server.common.dto.ErrorDetail;
import HookKiller.server.common.exception.BaseErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;

@Getter
@AllArgsConstructor
public enum UserException implements BaseErrorCode {
ALREADY_EXIST_USER_ERROR(BAD_REQUEST.value(), "User_400_1", "이미 회원인 유저입니다."),
USER_NOT_FOUND_ERROR(NOT_FOUND.value(), "User_404_1", "유저를 찾을 수 없습니다."),
ALREADY_REGISTER_USER_ID_ERROR(BAD_REQUEST.value(), "User_400_2", "이미 등록된 유저 아이디입니다."),
;

private final Integer statusCode;
private final String errorCode;
private final String reason;

@Override
public ErrorDetail getErrorDetail() {
return ErrorDetail.of(statusCode, errorCode, reason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByNickName(String nickname);

Optional<User> findByEmailAndPassword(String email, String password);

boolean existsByEmail(String email);
}
33 changes: 26 additions & 7 deletions src/main/java/HookKiller/server/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
package HookKiller.server.user.service;

import HookKiller.server.user.dto.request.UserLoginDto;
import HookKiller.server.auth.dto.request.RegisterRequest;
import HookKiller.server.auth.dto.response.AuthResponse;
import HookKiller.server.jwt.JwtTokenProvider;
import HookKiller.server.user.entity.User;
import HookKiller.server.user.exception.AlreadyExistUserException;
import HookKiller.server.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class UserService {


private final JwtTokenProvider jwtTokenProvider;
private final UserRepository userRepository;

public void signUp(UserLoginDto userLoginDto) throws Exception {
if (userRepository.findByEmail(userLoginDto.getEmail()).isPresent()) {
throw new Exception("이미 존재하는 이메일입니다.");

@Transactional
public ResponseEntity<AuthResponse> registerUser(RegisterRequest request) {
if (userRepository.existsByEmail(request.getEmail())) {
throw AlreadyExistUserException.EXCEPTION;
}

User user = userRepository.save(User.builder()
.email(request.getEmail())
.password(request.getPassword())
.nickName(request.getNickName())
.role(request.getRole())
.build());

AuthResponse res = AuthResponse.builder()
.token(jwtTokenProvider.generateToken(user.getId(), user.getEmail(),user.getNickName(), user.getRole()))
.build();

return ResponseEntity.ok(res);
}
}
Loading