Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
auth: register users
app/controllers/AuthController.php | 32 ++++++++++++++++++++++++++++++++ app/daos/UserDAO.php | 15 +++++++++++++++ app/services/AuthService.php | 6 ++++++ config/routes.php | 2 ++
diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index 0548724182694cb30d470220b819ebb8b8fe8031..dbc9c9310e0d8c6e0c1931bc65f0986e81f34712 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -27,4 +27,36 @@ // TODO: Flash messages echo 'Usuário ou senha inválidos'; } } + + public function register() { + $email = $_POST['email']; + $userName = $_POST['username']; + $password = $_POST['password']; + $passwdConfirmation = $_POST['password_confirmation']; + + if ($password != $passwdConfirmation) { + // TODO: Flash messages + echo 'As senhas não conferem'; + return; + } + + if (empty($email) || empty($password) || empty($userName)) { + throw new Exception("Email, username or password cannot be empty."); + } + + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { + throw new Exception("Invalid email format."); + } + + $cryptographicPassword = password_hash($password, PASSWORD_DEFAULT); + $user = $this->authService->register($userName, $email, $cryptographicPassword); + + if ($user) { + header('Location: /login'); + exit(); + } else { + // TODO: Flash messages + echo 'Erro ao cadastrar usuário'; + } + } } diff --git a/app/daos/UserDAO.php b/app/daos/UserDAO.php index b4f280614af57b66f3b7d59a3613cae96eed5e33..4344d85856d0874deb38a8098c2c12e19c7c4af7 100644 --- a/app/daos/UserDAO.php +++ b/app/daos/UserDAO.php @@ -30,4 +30,19 @@ } return null; } + + public function create(User $user) { + $sql = 'INSERT INTO users (username, email, encrypted_password) VALUES (:username, :email, :encrypted_password)'; + + + $stmt = $this->db->prepare($sql); + $stmt->bindValue(':username', $user->getUserName()); + $stmt->bindValue(':email', $user->getEmail()); + $stmt->bindValue(':encrypted_password', $user->getEncryptedPassword()); + $stmt->execute(); + + $user->setId($this->db->lastInsertId()); + + return $user; + } } diff --git a/app/services/AuthService.php b/app/services/AuthService.php index a2ba78eb5e1f576734c3986ba2e1cf41f5e68080..096f728afcf46a9c5fe30bb6df36882e733c8d97 100644 --- a/app/services/AuthService.php +++ b/app/services/AuthService.php @@ -18,4 +18,10 @@ } return null; } + + public function register($userName, $email, $password) { + $user = new User(null, $userName, $email, $password); + + return $this->userDAO->create($user); + } } diff --git a/config/routes.php b/config/routes.php index 6b76c286873fa7ceaf532c23d2f9ad8630287d13..2480e43aac27f2c4d03bc48f5eaa618c2e8a42d3 100644 --- a/config/routes.php +++ b/config/routes.php @@ -15,6 +15,8 @@ } elseif ($uri === 'login' && $method === 'POST') { return (new AuthController())->login(); } elseif ($uri === 'login') { return (new HelloController())->login(); + } elseif ($uri === 'register' && $method === 'POST') { + return (new AuthController())->register(); } elseif ($uri === 'register') { return (new HelloController())->register(); }