From 4d56d86b44cee56ecf68379d0ed99c2010e20583 Mon Sep 17 00:00:00 2001 From: Vitalii Martynov Date: Wed, 30 Oct 2024 18:19:31 +0200 Subject: [PATCH 1/2] AuthenticationService is implemented --- .../mate/academy/service/AuthenticationService.java | 10 +++++++++- src/main/java/mate/academy/service/UserService.java | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index 8f7c7c97..d0a8d4ac 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -1,6 +1,10 @@ package mate.academy.service; +import mate.academy.model.User; + public class AuthenticationService { + private static final UserService 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 +15,10 @@ public class AuthenticationService { * Return false in any other cases. */ public boolean login(String email, String password) { - return false; + User user = userService.findByEmail(email); + if (user == null) { + return false; + } + return user.getPassword().equals(password); } } diff --git a/src/main/java/mate/academy/service/UserService.java b/src/main/java/mate/academy/service/UserService.java index 1652d7d6..94210665 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 254fcfc7fd821e855de9462a0f3a197a1025c2de Mon Sep 17 00:00:00 2001 From: Vitalii Martynov Date: Thu, 31 Oct 2024 10:50:23 +0200 Subject: [PATCH 2/2] suggested changes were implemented --- .../java/mate/academy/service/AuthenticationService.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/mate/academy/service/AuthenticationService.java b/src/main/java/mate/academy/service/AuthenticationService.java index d0a8d4ac..b41163a5 100644 --- a/src/main/java/mate/academy/service/AuthenticationService.java +++ b/src/main/java/mate/academy/service/AuthenticationService.java @@ -3,7 +3,7 @@ import mate.academy.model.User; public class AuthenticationService { - private static final UserService userService = new UserService(); + private final UserService userService = new UserService(); /** * Imagine that some user wants to login to your site. @@ -16,9 +16,6 @@ public class AuthenticationService { */ public boolean login(String email, String password) { User user = userService.findByEmail(email); - if (user == null) { - return false; - } - return user.getPassword().equals(password); + return user != null && user.getPassword().equals(password); } }