add password login
This commit is contained in:
parent
93f5f022e4
commit
ebfe147ca4
12 changed files with 192 additions and 6 deletions
|
|
@ -8,5 +8,6 @@ final readonly class CreateUserDto
|
|||
{
|
||||
public function __construct(
|
||||
public EmailAddress $email,
|
||||
public string $password,
|
||||
) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue