Skip to content

Commit c5b9405

Browse files
committed
Hibernate user service task done
1 parent fab1265 commit c5b9405

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

src/main/java/mate/academy/Main.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public static void main(String[] args) {
6565
String incorrectEmail = "incorrect@email.com";
6666
String correctPassword = "correctPassword";
6767
String incorrectPassword = "incorrectPassword";
68+
String emptyLine = "";
6869
AuthenticationService authenticationService =
6970
(AuthenticationService) injector.getInstance(AuthenticationService.class);
7071

@@ -74,6 +75,12 @@ public static void main(String[] args) {
7475
throw new RuntimeException(e);
7576
}
7677

78+
try {
79+
authenticationService.register(emptyLine, correctPassword);
80+
} catch (RegistrationException e) {
81+
throw new RuntimeException(e);
82+
}
83+
7784
try {
7885
authenticationService.login(incorrectEmail, correctPassword);
7986
} catch (AuthenticationException e) {

src/main/java/mate/academy/model/User.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mate.academy.model;
22

3+
import jakarta.persistence.Column;
34
import jakarta.persistence.Entity;
45
import jakarta.persistence.GeneratedValue;
56
import jakarta.persistence.GenerationType;
@@ -12,7 +13,9 @@ public class User {
1213
@Id
1314
@GeneratedValue(strategy = GenerationType.IDENTITY)
1415
private Long id;
16+
@Column(nullable = false, unique = true)
1517
private String email;
18+
@Column(nullable = false)
1619
private String password;
1720
private byte[] salt;
1821

src/main/java/mate/academy/service/impl/AuthenticationServiceImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public User login(String email, String password) throws AuthenticationException
3333

3434
@Override
3535
public User register(String email, String password) throws RegistrationException {
36+
if (email.isEmpty()) {
37+
throw new RegistrationException("email can not be empty");
38+
}
3639
Optional<User> userFromDbOptional = userService.findByEmail(email);
3740
if (userFromDbOptional.isPresent()) {
3841
throw new RegistrationException("User with email: " + email + "already exists");

src/main/java/mate/academy/util/HashUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public static String hashPassword(String password, byte[] salt) {
2828
hashedPassword.append(String.format("%02x", b));
2929
}
3030
} catch (NoSuchAlgorithmException e) {
31-
throw new IllegalStateException("Could not create hash using SHA-512: "
32-
+ CRYPTO_AlGORITHM, e);
31+
throw new IllegalStateException("Could not create hash using: "
32+
+ CRYPTO_AlGORITHM + " algorythm ", e);
3333
}
3434
return hashedPassword.toString();
3535
}

0 commit comments

Comments
 (0)