add password login

This commit is contained in:
Yisroel Baum 2026-07-31 11:38:05 +03:00
parent 93f5f022e4
commit ebfe147ca4
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
12 changed files with 192 additions and 6 deletions

View file

@ -8,5 +8,6 @@ final readonly class CreateUserDto
{
public function __construct(
public EmailAddress $email,
public string $password,
) {}
}

View file

@ -3,6 +3,7 @@
namespace App\User;
use App\Shared\ValueObject\EmailAddress;
use Illuminate\Support\Facades\Hash;
class EloquentUserRepository implements UserRepository
{
@ -10,6 +11,7 @@ class EloquentUserRepository implements UserRepository
{
$model = UserModel::create([
'email' => $dto->email->value(),
'password' => Hash::make($dto->password),
]);
return $this->toDomain($model);
@ -25,6 +27,24 @@ class EloquentUserRepository implements UserRepository
return $this->toDomain($model);
}
public function findByCredentials(
EmailAddress $email,
string $password,
): ?User
{
$model = UserModel::query()
->where('email', $email->value())
->first();
if (
$model === null
|| ! Hash::check($password, $model->password)
) {
return null;
}
return $this->toDomain($model);
}
private function toDomain(UserModel $model): User
{
return new User(

View file

@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $email
* @property string $password
*
* @method static Builder<static>|UserModel newModelQuery()
* @method static Builder<static>|UserModel newQuery()
@ -16,7 +17,7 @@ use Illuminate\Database\Eloquent\Model;
*
* @mixin \Eloquent
*/
#[Fillable(['email'])]
#[Fillable(['email', 'password'])]
class UserModel extends Model
{
protected $table = 'users';

View file

@ -2,9 +2,16 @@
namespace App\User;
use App\Shared\ValueObject\EmailAddress;
interface UserRepository
{
public function create(CreateUserDto $dto): User;
public function find(int $id): ?User;
public function findByCredentials(
EmailAddress $email,
string $password,
): ?User;
}