if ($_SERVER['REQUEST_METHOD'] === 'POST') { $email = $_POST['email']; $password = $_POST['password']; $auth = new \Delight\Auth\Auth($pdo); try { $auth->loginWithUsername($email, $password); // user is logged in } catch (\Delight\Auth\InvalidEmailException $e) { // email not found } catch (\Delight\Auth\InvalidPasswordException $e) { // incorrect password } }
if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; $auth = new \Delight\Auth\Auth($pdo); try { $userId = $auth->register($email, $password, $name); // user account created and logged in } catch (\Delight\Auth\InvalidEmailException $e) { // invalid email address } catch (\Delight\Auth\InvalidPasswordException $e) { // invalid password } catch (\Delight\Auth\UserAlreadyExistsException $e) { // email address already registered } catch (\Delight\Auth\TooManyRequestsException $e) { // too many requests } }In both examples, the \Delight\Auth\Auth class is used to handle the authentication process. This class is part of the DelightPHP/PHP-Auth package, which is a popular and widely-used library for user authentication in PHP. Overall, using a package like PHP Users can save developers a lot of time and effort in implementing user authentication features in their PHP projects. It provides a standardized and secure way to handle user management tasks, which can be challenging to do from scratch.