diff --git a/database/migrations/structure/20230409_0101_username.sql b/database/migrations/structure/20230409_0101_username.sql new file mode 100644 index 0000000..adfcb72 --- /dev/null +++ b/database/migrations/structure/20230409_0101_username.sql @@ -0,0 +1,3 @@ +ALTER TABLE `users` +ADD `username` varchar(100) DEFAULT NULL, +ADD UNIQUE `username` (`username`); diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index 34baae4..7a83d8c 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -109,11 +109,11 @@ class LoginController return new JsonContent(['success' => true]); } - $user = $this->userRepository->getByEmail($this->request->post('email')); + $user = $this->userRepository->getByEmailOrUsername($this->request->post('email')); if ($user === null || !$user->checkPassword($this->request->post('password'))) { return new JsonContent([ 'error' => [ - 'errorText' => 'No user found with the given email address or the given password is wrong. You can request->post('email')) . '" title="Request password reset">request password reset!' ] ]); @@ -200,11 +200,11 @@ class LoginController } } - $user = $this->userRepository->getByEmail($this->request->post('email')); + $user = $this->userRepository->getByEmailOrUsername($this->request->post('email')); if ($user === null) { return new JsonContent([ 'error' => [ - 'errorText' => 'No user found with the given email address.' + 'errorText' => 'No user found with the given email address / username.' ] ]); } diff --git a/src/PersistentData/Model/User.php b/src/PersistentData/Model/User.php index 1a284d4..fb388bb 100644 --- a/src/PersistentData/Model/User.php +++ b/src/PersistentData/Model/User.php @@ -8,12 +8,14 @@ class User extends Model implements IUser { protected static string $table = 'users'; - protected static array $fields = ['email', 'password', 'type', 'google_sub', 'created']; + protected static array $fields = ['email', 'username', 'password', 'type', 'google_sub', 'created']; private static array $types = ['user', 'admin']; private string $email = ''; + private ?string $username = null; + private ?string $password = null; private string $type = 'user'; @@ -27,6 +29,11 @@ class User extends Model implements IUser $this->email = $email; } + public function setUsername(string $username): void + { + $this->username = $username; + } + public function setPassword(?string $hashedPassword): void { $this->password = $hashedPassword; @@ -64,6 +71,11 @@ class User extends Model implements IUser return $this->email; } + public function getUsername(): ?string + { + return $this->username; + } + public function getPassword(): ?string { return $this->password; diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index cc03b44..ebe5462 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -27,6 +27,24 @@ class UserRepository implements IUserRepository return $this->pdm->selectFromDb($select, User::class); } + public function getByUsername(string $username): ?User + { + $select = new Select(\Container::$dbConnection); + $select->where('username', '=', $username); + + return $this->pdm->selectFromDb($select, User::class); + } + + public function getByEmailOrUsername(string $emailOrUsername): ?User + { + $user = $this->getByEmail($emailOrUsername); + if ($user !== null) { + return $user; + } + + return $this->getByUsername($emailOrUsername); + } + public function getByGoogleSub(string $sub): ?User { $select = new Select(\Container::$dbConnection); diff --git a/views/account/account.php b/views/account/account.php index 315c71b..6a26290 100644 --- a/views/account/account.php +++ b/views/account/account.php @@ -25,6 +25,7 @@