Skip to content

Conversation

Nazar1106
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Thank you for your hard work on this project! There are a few critical issues that need to be addressed before we can approve the pull request:

  1. Unique Email Constraint: Ensure the email field in the User model is unique in the database to meet the task requirements.

  2. AuthenticationService Implementation: The login method should authenticate an existing user by checking if the email exists and verifying the password. It should not create a new user if the email is not found.

  3. Registration Logic: The register method should create a new user if the email does not already exist, rather than incorrectly calling the login method.

These issues impact the core functionality of user authentication and registration, which are critical to the task. Please address these points, and feel free to reach out if you need any help. Keep up the great work! 🚀

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review button in the Reviewers tab.

Comment on lines +25 to +26
private static final String FIRST_HALL_CAPACITY = "first hall with capacity 100";
private static final String SECOND_HALL_CAPACITY = "second hall with capacity 200";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variables FIRST_HALL_CAPACITY and SECOND_HALL_CAPACITY are used to store descriptions, but their names suggest they are for capacity values. Consider renaming these variables to better reflect their purpose, such as FIRST_HALL_DESCRIPTION and SECOND_HALL_DESCRIPTION.

+ "street racing, heists, and spies.";
private static final String FIRST_HALL_CAPACITY = "first hall with capacity 100";
private static final String SECOND_HALL_CAPACITY = "second hall with capacity 200";
private static final long LONG_1 = 1L;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constant LONG_1 is used to represent a day offset for date calculations. Consider renaming it to something more descriptive, like ONE_DAY_OFFSET, to improve code readability.

private static final long LONG_1 = 1L;
private static final String USER_PASSWORD = "nazar1177";
private static final String USER_EMAIL = "nazar@gmail.com";
private static final String EMAIL_NOT_EXIST = "nazarss@gmail.com";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable EMAIL_NOT_EXIST is declared but never used in the code. If it's not needed, consider removing it to clean up the code.

Comment on lines 32 to 33
if (transaction != null) {
transaction.rollback();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transaction rollback is correctly handled, but consider logging the exception before throwing a DataProcessingException to aid in debugging.


@Override
public User add(User user) {
Session session = null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The session variable is declared outside the try block, which is not necessary. You can declare it inside the try block to limit its scope and improve code readability.

Comment on lines 72 to 73
int result = Objects.hash(getId(), getEmail(), getPassword());
result = 31 * result + Arrays.hashCode(getSalt());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the hashCode method, consider using Objects.hash(id, email, password) directly without the getter methods for consistency with the equals method.

Comment on lines 19 to 30
public User login(String email, String password) throws AuthenticationException {
User user = new User();

Optional<User> userByEmail = userService.findByEmail(email);
if (userByEmail.isEmpty()) {
user.setEmail(email);
user.setPassword(password);
return user;
} else {
throw new AuthenticationException("User with email " + email + " already exist");
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The login method logic is incorrect. It should authenticate an existing user by checking if the email exists and then verifying the password. Currently, it throws an exception if the user exists, which is not the expected behavior for a login method.

Comment on lines 33 to 40
public User register(String email, String password) throws RegistrationException {
try {
User userLogin = login(email, password);
return userService.add(userLogin);
} catch (Exception e) {
throw new RegistrationException("User can't register ", e);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The register method incorrectly calls the login method. Registration should create a new user if the email does not already exist. Consider checking if the user exists first, and if not, create and add the new user.

Comment on lines +19 to +22
user.setSalt(HashUtil.salt());
String userHashPassword =
HashUtil.hashPassword(user.getPassword(), user.getSalt());
user.setPassword(userHashPassword);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add method correctly hashes the user's password with a salt. Ensure that the HashUtil.salt() method generates a secure and random salt for each user to maintain security.

@@ -5,6 +5,7 @@

public class HibernateUtil {
private static final SessionFactory sessionFactory = initSessionFactory();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a shutdown method to close the SessionFactory when the application is stopped. This can help in releasing resources properly.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your submission! 🎉 Your code demonstrates a solid understanding of setting up services and models in a Java application. However, there are a few critical issues that need to be addressed to ensure the functionality and security of your application:

  1. Unique Email Constraint: Ensure the User model has a unique constraint on the email field by adding @Column(unique = true). This will enforce email uniqueness at the database level.

  2. AuthenticationService Implementation:

    • The login method should handle cases where the user is not found by checking if the Optional is present. Use user.orElseThrow() to avoid NoSuchElementException.
    • The register method should hash the password using HashUtil.hashPassword(password, salt) and generate a salt before saving the user. Ensure the email does not already exist before creating a new user.
  3. Database Query Handling: Replace getSingleResultOrNull() with getResultList() and check if the list is empty to ensure compatibility across different environments.

These improvements are crucial for the security and reliability of your application. While the decision is to APPROVE your pull request to help you move forward with your learning, please address these issues in future iterations to enhance your code quality. Keep up the good work and continue learning from these experiences! 🚀

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

AuthenticationService authenticationService = (AuthenticationService)
injector.getInstance(AuthenticationService.class);

User login = authenticationService.login(USER_EMAIL, USER_PASSWORD);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The login method should authenticate an existing user by checking if the email exists and verifying the password. It should not create a new user if the email is not found. Please ensure the login method in AuthenticationService is implemented correctly according to the task requirements.


User login = authenticationService.login(USER_EMAIL, USER_PASSWORD);
System.out.println(login);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The register method should create a new user only if the email does not already exist. Ensure that the register method in AuthenticationService checks for the existence of the email before creating a new user.

Query<User> userQuery =
session.createQuery("SELECT u FROM User u where u.email=:email", User.class);
userQuery.setParameter(EMAIL, email);
User user = userQuery.getSingleResultOrNull();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method getSingleResultOrNull() is not a standard JPA method and might not be supported in all environments. Consider using getResultList() and checking if the list is empty to handle the case where no user is found.

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String email;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To ensure the email field is unique in the database, consider adding a unique constraint annotation, such as @Column(unique = true), to the email field. This will enforce the uniqueness of email addresses at the database level.


@Override
public User login(String email, String password) throws AuthenticationException {
Optional<User> user = userService.findByEmail(email);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The login method should handle the case where the user is not found. Before accessing user.get(), check if the Optional is present to avoid NoSuchElementException. Consider using user.orElseThrow() with a custom exception message.

Optional<User> userByEmail = userService.findByEmail(email);
if (userByEmail.isEmpty()) {
user.setEmail(email);
user.setPassword(password);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The register method should hash the password before saving the user. Use HashUtil.hashPassword(password, salt) to hash the password and set it to the user object. Ensure that a salt is generated and stored with the user.

Comment on lines +49 to +52
if (user == null) {
return Optional.empty();
}
return Optional.of(user);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (user == null) {
return Optional.empty();
}
return Optional.of(user);
return Optional.ofNullable(user);

Comment on lines +15 to +16
private static final String CAN_T_ADD_NEW_USER_MSG = "Can't add new user";
private static final String SPACE = " ";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final String CAN_T_ADD_NEW_USER_MSG = "Can't add new user";
private static final String SPACE = " ";
private static final String CAN_T_ADD_NEW_USER_MSG = "Can't add new user ";

import java.util.Arrays;
import java.util.Objects;

@Entity

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add Table annotation

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String email;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the email unique. We need to check the email during user registration and make the email a unique field in the database.

@Override
public User login(String email, String password) throws AuthenticationException {
Optional<User> user = userService.findByEmail(email);
String userHashPassword = HashUtil.hashPassword(password, user.get().getSalt());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String userHashPassword = HashUtil.hashPassword(password, user.get().getSalt());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants