scan.fyi/app/Models/User.php
2026-02-22 17:49:23 +00:00

73 lines
1.6 KiB
PHP
Executable file

<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email_hash',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'email_hash',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
];
}
/**
* Hash an email address using HMAC-SHA256.
*/
public static function hashEmail(string $email): string
{
return hash_hmac('sha256', strtolower(trim($email)), config('app.key'));
}
/**
* Find a user by their email address.
*/
public static function findByEmail(string $email): ?self
{
$emailHash = self::hashEmail($email);
return self::where('email_hash', $emailHash)->first();
}
/**
* Get the magic login tokens for the user.
*/
public function magicLoginTokens(): HasMany
{
return $this->hasMany(MagicLoginToken::class, 'email_hash', 'email_hash');
}
}