From 2b75ea2dea2a1977ad649f993c31f1b1ad2454cb Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Sun, 16 Jun 2024 21:26:10 -0400 Subject: [PATCH] Add the Login action. --- app/src/Controller/User/LoginUserAction.php | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 app/src/Controller/User/LoginUserAction.php diff --git a/app/src/Controller/User/LoginUserAction.php b/app/src/Controller/User/LoginUserAction.php new file mode 100644 index 0000000..c3f41cb --- /dev/null +++ b/app/src/Controller/User/LoginUserAction.php @@ -0,0 +1,40 @@ +request->getParsedBody(); + + try { + $user = $this->userRepository->getFromEmail($credentials['email']); + } catch (EntityNotFoundException $e) { + $this->session->getFlash()->add('error', "Unable to login user."); + return $this->response->withStatus(400); + } + + if (!$this->auth->verify($credentials['password'], $user->getPassword())) { + $this->session->getFlash()->add('error', 'Unable to login.'); + return $this->response->withStatus(400); + } + + // start the session + $this->session->set('user', [ 'id' => $user->getId() ]); + return $this->response; + } +}