Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
auth: bootstrap authentication logic, daos and services
app/controllers/AuthController.php | 27 ++++++++++++++++++++++++++ app/daos/UserDAO.php | 33 ++++++++++++++++++++++++++++++++ app/models/User.php | 31 ++++++++++++++++++++++++++++++ app/services/AuthService.php | 21 ++++++++++++++++++++ config/routes.php | 4 +++
diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php new file mode 100644 index 0000000000000000000000000000000000000000..fda6afafaeb0ce30c28c87871e15afc910e8b91a --- /dev/null +++ b/app/controllers/AuthController.php @@ -0,0 +1,27 @@ +<?php + +require_once __DIR__ . '/../services/AuthService.php'; + +class AuthController { + private $authService; + + public function __construct() { + $this->authService = new AuthService(); + } + + public function login() { + $email = $_POST['email']; + $password = $_POST['password']; + + $cryptographicPassword = password_hash($password, PASSWORD_DEFAULT); + + $user = $this->authService->login($email, $cryptographicPassword); + + if ($user) { + //$_SESSION['user_id'] = $user['id']; + echo 'Usuário logado com sucesso'; + } else { + echo 'Usuário ou senha inválidos'; + } + } +} diff --git a/app/daos/UserDAO.php b/app/daos/UserDAO.php new file mode 100644 index 0000000000000000000000000000000000000000..8cb7b6985d14f1de8cabd7aac29cf4ba967a733e --- /dev/null +++ b/app/daos/UserDAO.php @@ -0,0 +1,33 @@ +<?php + +require_once __DIR__ . '/../../config/database.php'; +require_once __DIR__ . '/../models/User.php'; + +class UserDAO { + private $db; + + public function __construct() { + $this->db = getDatabaseConnection(); + } + + public function findByEmail($email) { + $sql = 'SELECT * FROM users WHERE email = :email'; + + $stmt = $this->db->prepare($sql); + $stmt->bindParam(':email', $email); + $stmt->execute(); + + $userData = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($userData) { + return new User( + $userData['id'], + $userData['name'], + $userData['email'], + $userData['password'] + ); + } + + return null; + } +} diff --git a/app/models/User.php b/app/models/User.php new file mode 100644 index 0000000000000000000000000000000000000000..25d7845fab506733857cc6c3a7367e52639a2689 --- /dev/null +++ b/app/models/User.php @@ -0,0 +1,31 @@ +<?php + +class User { + private $id; + private $name; + private $email; + private $encryptedPassword; + + public function __construct($id, $name, $email, $encryptedPassword) { + $this->id = $id; + $this->name = $name; + $this->email = $email; + $this->encryptedPassword = $encryptedPassword; + } + + public function validatePassword($password) { + return password_verify($password, $this->encryptedPassword); + } + + public function getId() { + return $this->id; + } + + public function getName() { + return $this->name; + } + + public function getEmail() { + return $this->email; + } +} diff --git a/app/services/AuthService.php b/app/services/AuthService.php new file mode 100644 index 0000000000000000000000000000000000000000..a2ba78eb5e1f576734c3986ba2e1cf41f5e68080 --- /dev/null +++ b/app/services/AuthService.php @@ -0,0 +1,21 @@ +<?php + +require_once __DIR__ . '/../daos/UserDAO.php'; + +class AuthService { + private $userDAO; + + public function __construct() { + $this->userDAO = new UserDAO(); + } + + public function login($email, $password) { + $user = $this->userDAO->findByEmail($email); + + if ($user && $user->validatePassword($password)) { + return $user; + } + + return null; + } +} diff --git a/config/routes.php b/config/routes.php index 144e60365e445a650748b526bc22c11d798ac24b..6b76c286873fa7ceaf532c23d2f9ad8630287d13 100644 --- a/config/routes.php +++ b/config/routes.php @@ -2,13 +2,17 @@ // TODO: Usar um autoload aqui require_once '../app/controllers/HelloController.php'; +require_once '../app/controllers/AuthController.php'; return function() { $uri = trim($_SERVER['REQUEST_URI'], '/'); + $method = $_SERVER['REQUEST_METHOD']; // TODO: Meio burro isso if ($uri === '') { return (new HelloController())->index(); + } elseif ($uri === 'login' && $method === 'POST') { + return (new AuthController())->login(); } elseif ($uri === 'login') { return (new HelloController())->login(); } elseif ($uri === 'register') {