Skip to content

Commit

Permalink
Merge pull request #8 from Leets-Official/feat/#7/로그인-구현
Browse files Browse the repository at this point in the history
[Feat] 로그인 구현
  • Loading branch information
ehs208 authored Oct 10, 2024
2 parents c357a7f + 1abfed7 commit 76ebe51
Show file tree
Hide file tree
Showing 25 changed files with 984 additions and 18 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ jobs:
echo "${{ secrets.YML_DEV }}" > ./application-dev.yml
shell: bash

# 환경별 yml 파일 생성(3) - jwt
- name: make application-jwt.yml
if: contains(github.ref, 'develop')
run: |
cd ./src/main/resources
touch ./application-jwt.yml
echo "${{ secrets.YML_JWT }}" > ./application-jwt.yml
shell: bash

# gradle build
- name: Build with Gradle
run: ./gradlew build -x test
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ build/
application.yaml
application-dev.yaml
application-local.yaml
application-jwt.yaml

### macOS ###
.DS_Store
.AppleDouble
.LSOverride

### STS ###
.apt_generated
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar

# 운영 및 개발에서 사용되는 환경 설정을 분리
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=dev", "/app.jar"]
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=dev,jwt", "/app.jar"]
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.0'

}

tasks.named('test') {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/leets/xcellentbe/XcellentBeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing
public class XcellentBeApplication {

public static void main(String[] args) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/leets/xcellentbe/domain/user/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.leets.xcellentbe.domain.user;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum Role {
GUEST("ROLE_GUEST", "비회원"),
USER("ROLE_USER", "회원");

private final String key;
private final String title;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
package com.leets.xcellentbe.domain.user.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 com.leets.xcellentbe.domain.user.dto.UserLoginRequestDto;
import com.leets.xcellentbe.domain.user.dto.UserSignUpRequestDto;
import com.leets.xcellentbe.domain.user.service.UserService;
import com.leets.xcellentbe.global.response.GlobalResponseDto;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/user")
@RequestMapping("/api/user")
@RequiredArgsConstructor
public class UserController {
// private final UserService userService;
//
// @PostMapping("/auth/register")
// @Operation(summary = "회원가입", description = "회원가입을 합니다.")
// public ResponseEntity<GlobalResponseDto<String>> register(@RequestBody UserSignUpRequestDto userSignUpRequestDto) {
// return ResponseEntity.status(HttpStatus.CREATED)
// .body(GlobalResponseDto.success(userService.register(userSignUpRequestDto), HttpStatus.CREATED.value()));
// }
//
// @Operation(summary = "로그인", description = "사용자의 이메일과 비밀번호로 로그인합니다.")
// @PostMapping("/auth/login")
// public String login(@RequestBody UserLoginRequestDto userLoginRequestDto) {
// // 로그인 로직 처리
// return "로그인 성공";
// }
}
59 changes: 58 additions & 1 deletion src/main/java/com/leets/xcellentbe/domain/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.leets.xcellentbe.domain.user.domain;

import java.time.LocalDateTime;
import java.time.LocalDate;

import org.springframework.security.crypto.password.PasswordEncoder;

import com.leets.xcellentbe.domain.shared.BaseTimeEntity;
import com.leets.xcellentbe.domain.shared.UserStatus;


import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -30,8 +38,9 @@ public class User extends BaseTimeEntity {

@NotNull
@Column(length = 100)
private String username;
private String userName;

@NotNull
private String password;

@Column
Expand Down Expand Up @@ -63,5 +72,53 @@ public class User extends BaseTimeEntity {

@NotNull
@Column
@Enumerated(EnumType.STRING)
private UserStatus userStatus;

@NotNull
@Column(length=4)
private int userBirthYear;

@Column(length=2)
@NotNull
private int userBirthMonth;

@Column(length=2)
@NotNull
private int userBirthDay;

@Builder
private User(String customId, String email, String userName, String password, String phoneNumber, String description, int userBirthDay, int userBirthMonth, int userBirthYear) {
this.customId = customId;
this.email = email;
this.userName = userName;
this.password = password;
this.phoneNumber= phoneNumber;
this.description = description;
this.userStatus = UserStatus.ACTIVE;
this.userBirthMonth = userBirthMonth;
this.userBirthYear = userBirthYear;
this.userBirthDay = userBirthDay;
}

public void passwordEncode(PasswordEncoder passwordEncoder) { //비밀번호 암호화 메소드
this.password = passwordEncoder.encode(this.password);
}

public static User create(String customId, String email, String userName, String password, String phoneNumber, int userBirthMonth, int userBirthYear, int userBirthDay) {
return User.builder()
.customId(customId)
.email(email)
.userName(userName)
.password(password)
.phoneNumber(phoneNumber)
.userBirthDay(userBirthDay)
.userBirthYear(userBirthYear)
.userBirthMonth(userBirthMonth)
.build();
}

public void updateRefreshToken(String updateRefreshToken) {
this.refreshToken = updateRefreshToken;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.leets.xcellentbe.domain.user.domain.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.leets.xcellentbe.domain.user.domain.User;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
Optional<User> findByCustomId(String customId);;
Optional<User> findByRefreshToken(String refreshToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.leets.xcellentbe.domain.user.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class UserLoginRequestDto {
private String email;
private String password;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.leets.xcellentbe.domain.user.dto;

import java.time.LocalDate;

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class UserSignUpRequestDto {

private String email;
private String customId;
private String userName;
private String password;
private String phoneNumber;
private int userBirthYear;
private int userBirthMonth;
private int userBirthDay;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.leets.xcellentbe.domain.user.exception;

import com.leets.xcellentbe.global.error.ErrorCode;
import com.leets.xcellentbe.global.error.exception.CommonException;

public class UserAlreadyExistsException extends CommonException {
public UserAlreadyExistsException() {
super(ErrorCode.USER_ALREADY_EXISTS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public class UserNotFoundException extends CommonException {
public UserNotFoundException() {

super(ErrorCode.USER_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.leets.xcellentbe.domain.user.service;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.leets.xcellentbe.domain.user.domain.User;
import com.leets.xcellentbe.domain.user.exception.UserAlreadyExistsException;

import com.leets.xcellentbe.domain.user.domain.repository.UserRepository;
import com.leets.xcellentbe.domain.user.dto.UserSignUpRequestDto;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;

@Service
@Transactional
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;

public String register(UserSignUpRequestDto userSignUpRequestDto) {

if (userRepository.findByEmail(userSignUpRequestDto.getEmail()).isPresent()) {
throw new UserAlreadyExistsException();
}
if (userRepository.findByCustomId(userSignUpRequestDto.getCustomId()).isPresent()) {
throw new UserAlreadyExistsException();
}

User user = User.create(userSignUpRequestDto.getCustomId(), userSignUpRequestDto.getEmail(), userSignUpRequestDto.getUserName(),
userSignUpRequestDto.getPassword(), userSignUpRequestDto.getPhoneNumber(), userSignUpRequestDto.getUserBirthYear(), userSignUpRequestDto.getUserBirthDay(), userSignUpRequestDto.getUserBirthMonth());

user.passwordEncode(passwordEncoder);
userRepository.save(user);

return "회원가입이 완료되었습니다.";
}

}
Loading

0 comments on commit 76ebe51

Please sign in to comment.