-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from KUIT-Space/signup-seongjunnoh
Signup seongjunnoh
- Loading branch information
Showing
26 changed files
with
741 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package space.space_spring; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import space.space_spring.domain.User; | ||
|
||
import java.util.Date; | ||
|
||
@Component | ||
public class JwtProvider { | ||
|
||
@Value("${secret.jwt-secret-key}") | ||
private String JWT_TOKEN_KEY; | ||
|
||
@Value("${secret.jwt-expired-in}") | ||
private Long JWT_EXPIRATION_TIME; | ||
|
||
public String generateToken(User user) { | ||
Claims claims = Jwts.claims().setSubject(user.getEmail()); | ||
Date now = new Date(); | ||
Date expiration = new Date(now.getTime() + JWT_EXPIRATION_TIME); | ||
|
||
return Jwts.builder() | ||
.setClaims(claims) | ||
.setIssuedAt(now) | ||
.setExpiration(expiration) | ||
.claim("userId", user.getUserId()) | ||
.signWith(SignatureAlgorithm.HS256, JWT_TOKEN_KEY) | ||
.compact(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/space/space_spring/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package space.space_spring.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import space.space_spring.dto.PostUserLoginRequest; | ||
import space.space_spring.dto.PostUserLoginResponse; | ||
import space.space_spring.dto.PostUserSignupRequest; | ||
import space.space_spring.dto.PostUserSignupResponse; | ||
import space.space_spring.exception.UserException; | ||
import space.space_spring.response.BaseResponse; | ||
import space.space_spring.service.UserService; | ||
|
||
import static space.space_spring.response.status.BaseExceptionResponseStatus.INVALID_USER_VALUE; | ||
import static space.space_spring.util.BindingResultUtils.getErrorMessage; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/user") | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
/** | ||
* ํ์๊ฐ์ | ||
*/ | ||
@PostMapping("/signup") | ||
public BaseResponse<PostUserSignupResponse> signup(@Validated @RequestBody PostUserSignupRequest postUserSignupRequest, BindingResult bindingResult) { | ||
log.info("<UserController> Signup request: {}", postUserSignupRequest); | ||
if (bindingResult.hasErrors()) { | ||
throw new UserException(INVALID_USER_VALUE, getErrorMessage(bindingResult)); | ||
} | ||
return new BaseResponse<>(userService.signup(postUserSignupRequest)); | ||
} | ||
|
||
/** | ||
* ๋ก๊ทธ์ธ | ||
*/ | ||
@PostMapping("/login") | ||
public BaseResponse<PostUserLoginResponse> login(@Validated @RequestBody PostUserLoginRequest postUserLoginRequest, BindingResult bindingResult) { | ||
if (bindingResult.hasErrors()) { | ||
throw new UserException(INVALID_USER_VALUE, getErrorMessage(bindingResult)); | ||
} | ||
return new BaseResponse<>(userService.login(postUserLoginRequest)); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package space.space_spring.dao; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.NoResultException; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.persistence.TypedQuery; | ||
import org.springframework.stereotype.Repository; | ||
import space.space_spring.domain.User; | ||
import space.space_spring.dto.PostUserSignupRequest; | ||
|
||
@Repository | ||
public class UserDao { | ||
|
||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
public Long saveUser(PostUserSignupRequest postUserSignupRequest) { | ||
User user = new User(); | ||
user.saveUser(postUserSignupRequest.getEmail(), postUserSignupRequest.getPassword(), postUserSignupRequest.getUserName()); | ||
|
||
em.persist(user); | ||
return user.getUserId(); | ||
} | ||
|
||
public User findUserByEmail(String email) { | ||
TypedQuery<User> query = em.createQuery("SELECT u FROM User u WHERE u.email = :email", User.class); | ||
query.setParameter("email", email); | ||
return query.getSingleResult(); | ||
} | ||
|
||
public boolean hasDuplicateEmail(String email) { | ||
String jpql = "SELECT COUNT(u) FROM User u WHERE u.email = :email"; | ||
Long count = em.createQuery(jpql, Long.class).setParameter("email", email).getSingleResult(); | ||
return count > 0; | ||
} | ||
|
||
public User getUserByEmail(String email) { | ||
try { | ||
TypedQuery<User> query = em.createQuery("SELECT u FROM User u WHERE u.email = :email", User.class); | ||
query.setParameter("email", email); | ||
return query.getSingleResult(); | ||
} catch (NoResultException e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package space.space_spring.domain; | ||
|
||
import jakarta.persistence.*; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@MappedSuperclass | ||
public abstract class BaseEntity { | ||
|
||
// ๋ชจ๋ ์ํฐํฐ๊ฐ ๊ณตํต์ผ๋ก ๊ฐ์ ธ์ผํ ์์ฑ | ||
|
||
@Column(name = "created_date", updatable = false) | ||
private LocalDateTime createdAt; | ||
|
||
@Column(name = "updated_date") | ||
private LocalDateTime lastModifiedAt; | ||
|
||
@Column(name = "status") | ||
private String status; | ||
|
||
@PrePersist | ||
protected void onCreate() { | ||
createdAt = LocalDateTime.now(); | ||
lastModifiedAt = LocalDateTime.now(); | ||
status = "ACTIVE"; | ||
} | ||
|
||
@PreUpdate | ||
protected void onUpdate() { | ||
lastModifiedAt = LocalDateTime.now(); | ||
} | ||
|
||
protected void initializeBaseEntityFields() { | ||
onCreate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package space.space_spring.domain; | ||
|
||
import jakarta.annotation.Nullable; | ||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
|
||
@Entity | ||
@Getter | ||
@Table(name = "Spaces") | ||
public class Space extends BaseEntity { | ||
|
||
@Id @GeneratedValue | ||
@Column(name = "space_id") | ||
private Long spaceId; | ||
|
||
@Column(name = "space_name") | ||
private String spaceName; | ||
|
||
@Column(name = "space_profile_img") | ||
@Nullable | ||
private String spaceProfileImg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package space.space_spring.domain; | ||
|
||
import jakarta.annotation.Nullable; | ||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.Getter; | ||
import org.hibernate.validator.constraints.Length; | ||
|
||
@Entity | ||
@Table(name = "Users") | ||
@Getter | ||
public class User extends BaseEntity { | ||
|
||
@Id @GeneratedValue | ||
@Column(name = "user_id") | ||
private Long userId; | ||
|
||
@Column(name = "email") | ||
private String email; | ||
|
||
@Column(name = "password") | ||
private String password; | ||
|
||
@Column(name = "user_name") | ||
private String userName; | ||
|
||
@Column(name = "jwt") | ||
@Nullable | ||
private String jwt; | ||
|
||
public void saveUser(String email, String password, String userName) { | ||
this.email = email; | ||
this.password = password; | ||
this.userName = userName; | ||
initializeBaseEntityFields(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package space.space_spring.domain; | ||
|
||
import jakarta.annotation.Nullable; | ||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
|
||
@Entity | ||
@Getter | ||
@Table(name = "User_Space") | ||
public class UserSpace extends BaseEntity { | ||
|
||
@Id @GeneratedValue | ||
@Column(name = "user_space_id") | ||
private Long userSpaceId; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "space_id") | ||
private Space space; | ||
|
||
@Column(name = "user_name") | ||
private String userName; | ||
|
||
@Column(name = "user_profile_img") | ||
@Nullable | ||
private String userProfileImg; | ||
|
||
@Column(name = "user_profile_msg") | ||
@Nullable | ||
private String userProfileMsg; | ||
|
||
@Column(name = "user_auth") | ||
private String userAuth; | ||
|
||
@Column(name = "space_order") | ||
private int spaceOrder; | ||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/space/space_spring/dto/PostUserLoginRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package space.space_spring.dto; | ||
|
||
import jakarta.validation.constraints.Pattern; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class PostUserLoginRequest { | ||
|
||
@Pattern(regexp = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,6}$", message = "์ด๋ฉ์ผ ํ์์ ๋ง์ง ์์ต๋๋ค.") // ์ด๋ฉ์ผ ์ ๊ทํํ์ ํ์ธ ํ์ํจ | ||
private String email; | ||
|
||
@Pattern( | ||
regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,16}$", | ||
message = "8~16๊ธ์์ ์๋ฌธ ๋/์๋ฌธ์, ์ซ์, ํน์๋ฌธ์๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค." | ||
) | ||
private String password; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/space/space_spring/dto/PostUserLoginResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package space.space_spring.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class PostUserLoginResponse { | ||
|
||
private String jwt; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/space/space_spring/dto/PostUserSignupRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package space.space_spring.dto; | ||
|
||
import jakarta.validation.constraints.Pattern; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hibernate.validator.constraints.Length; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class PostUserSignupRequest { | ||
|
||
@Pattern(regexp = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,6}$", message = "์ด๋ฉ์ผ ํ์์ ๋ง์ง ์์ต๋๋ค.") // ์ด๋ฉ์ผ ์ ๊ทํํ์ ํ์ธ ํ์ํจ | ||
private String email; | ||
|
||
@Pattern( | ||
regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,16}$", | ||
message = "8~16๊ธ์์ ์๋ฌธ ๋/์๋ฌธ์, ์ซ์, ํน์๋ฌธ์๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค." | ||
) | ||
private String password; | ||
|
||
@Length(min = 1, max = 10, message = "์ด๋ฆ์ 10์์ด๋ด์ ๋ฌธ์์ด์ด์ด์ผ ํฉ๋๋ค.") | ||
private String userName; | ||
} |
Oops, something went wrong.