From fc904c6fe5b20fbbaadfffc5e3daa7cfa5c10bb1 Mon Sep 17 00:00:00 2001 From: Dima Davydov Date: Wed, 24 Jul 2024 13:57:06 +0300 Subject: [PATCH 1/2] AuthenticationService login method and findByEmail method are implemented --- .../mate/academy/service/AuthenticationService.java | 12 ++++++++++++ src/main/java/mate/academy/service/UserService.java | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 8f7c7c975..1b420fcc0 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,6 +1,14 @@ package mate.academy.service; +import mate.academy.model.User; + public class AuthenticationService { + private UserService userService; + + public AuthenticationService() { + userService = new UserService(); + } + /** * Imagine that some user wants to login to your site. * You should check if user credentials (login and password) are valid or not. @@ -11,6 +19,10 @@ public class AuthenticationService { * Return false in any other cases. */ public boolean login(String email, String password) { + User user = userService.findByEmail(email); + if (user != null && user.getPassword().equals(password)) { + return true; + } return false; } } diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 1652d7d68..4782a793f 100644 --- a/src/main/java/mate/academy/service/UserService.java +++ b/src/main/java/mate/academy/service/UserService.java @@ -15,6 +15,11 @@ public class UserService { * Return null if there is no suitable user */ public User findByEmail(String email) { + for (User user: users) { + if (user.getEmail().equals(email)) { + return user; + } + } return null; } } From 09cb551813fff50a49e45d176d13b5e40784753f Mon Sep 17 00:00:00 2001 From: Dima Davydov Date: Wed, 24 Jul 2024 14:52:32 +0300 Subject: [PATCH 2/2] added fixes after review --- .../java/mate/academy/service/AuthenticationService.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 1b420fcc0..bb0a241c2 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -20,9 +20,6 @@ public AuthenticationService() { */ public boolean login(String email, String password) { User user = userService.findByEmail(email); - if (user != null && user.getPassword().equals(password)) { - return true; - } - return false; + return user != null && user.getPassword().equals(password); } }