Skip to content

Commit

Permalink
feat(#30): User Entity 비밀번호 추가
Browse files Browse the repository at this point in the history
- 기존에는 Google OAuth를 이용해서 비밀번호를 사용하지 않다가, 시나브로 로그인/회원가입을 위해서 유저 비밀번호 컬럼을 추가했어요.
  • Loading branch information
cabbage16 committed Nov 27, 2024
1 parent 1766dfd commit 589ffbb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public TokenResponse execute(GoogleInformation information) {

if (user.isEmpty()) {
userRepository.save(
new User(email, information.getName(), Authority.USER)
User.builder()
.email(email)
.name(information.getName())
.authority(Authority.USER)
.build()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.bamdoliro.sinabro.domain.user.domain;

import com.bamdoliro.sinabro.domain.user.domain.type.Authority;
import com.bamdoliro.sinabro.domain.user.domain.value.Password;
import com.bamdoliro.sinabro.shared.entity.BaseTimeEntity;
import com.bamdoliro.sinabro.shared.util.PasswordUtil;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -26,13 +29,18 @@ public class User extends BaseTimeEntity {
@Column(nullable = false)
private String name;

@Embedded
private Password password;

@Enumerated(EnumType.STRING)
@Column(nullable = false, length = 10)
private Authority authority;

public User(String email, String name, Authority authority) {
@Builder
public User(String email, String name, String password, Authority authority) {
this.email = email;
this.name = name;
this.password = new Password(PasswordUtil.encode(password));
this.authority = authority;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bamdoliro.sinabro.domain.user.domain.value;

import com.bamdoliro.sinabro.shared.util.PasswordUtil;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Embeddable
public class Password {

@Column(name = "password", length = 60)
private String value;

public boolean match(String password) {
return PasswordUtil.matches(value, password);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/bamdoliro/sinabro/shared/util/PasswordUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bamdoliro.sinabro.shared.util;

import lombok.experimental.UtilityClass;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@UtilityClass
public class PasswordUtil {

private static final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

public boolean matches(String actual, String expected) {
return passwordEncoder.matches(expected, actual);
}

public String encode(String password) {
return passwordEncoder.encode(password);
}
}

0 comments on commit 589ffbb

Please sign in to comment.