add user manager and auth for backend
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
use Qbhy\HyperfAuth\Authenticatable;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $username
|
||||
* @property string $password
|
||||
* @property string $email
|
||||
* @property int $status
|
||||
* @property array $ext
|
||||
* @property string $refresh_token
|
||||
* @property \Carbon\Carbon $refresh_token_expires_at
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
*/
|
||||
class User extends Model implements Authenticatable
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'users';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [
|
||||
'username',
|
||||
'password',
|
||||
'email',
|
||||
'status',
|
||||
'ext',
|
||||
'refresh_token',
|
||||
'refresh_token_expires_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*/
|
||||
protected array $hidden = [
|
||||
'password',
|
||||
'refresh_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = [
|
||||
'id' => 'integer',
|
||||
'status' => 'integer',
|
||||
'ext' => 'array',
|
||||
'refresh_token_expires_at' => 'datetime',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the name of the unique identifier for the user.
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a user by their unique identifier.
|
||||
*/
|
||||
public static function retrieveById($key): ?Authenticatable
|
||||
{
|
||||
return static::query()->find($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user's password (auto-hash).
|
||||
*/
|
||||
public function setPasswordAttribute($value): void
|
||||
{
|
||||
$this->attributes['password'] = password_hash($value, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify the user's password.
|
||||
*/
|
||||
public function verifyPassword(string $password): bool
|
||||
{
|
||||
return password_verify($password, $this->password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if refresh token is valid.
|
||||
*/
|
||||
public function isRefreshTokenValid(): bool
|
||||
{
|
||||
if (!$this->refresh_token || !$this->refresh_token_expires_at) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->refresh_token_expires_at->isFuture();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user