updated tokens and emails
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class EmailUpdate extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'email',
|
||||
'token'
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the user that owns the email update.
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
87
app/Models/Token.php
Normal file
87
app/Models/Token.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Token extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'type',
|
||||
'data',
|
||||
'expires_at',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'expires_at' => 'datetime',
|
||||
'data' => 'array',
|
||||
];
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The primary key for the model is incrementing.
|
||||
*
|
||||
* @var bool $incrementing
|
||||
*/
|
||||
public $incrementing = false;
|
||||
|
||||
/**
|
||||
* The primary key type for the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $keyType = 'string';
|
||||
|
||||
/**
|
||||
* The "booted" method of the model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function ($model) {
|
||||
if (empty($model->{$model->getKeyName()}) === true) {
|
||||
do {
|
||||
$newToken = Str::random(48);
|
||||
} while (self::where($model->getKeyName(), $newToken)->exists());
|
||||
|
||||
$model->{$model->getKeyName()} = $newToken;
|
||||
}
|
||||
|
||||
if (empty($model->expires_at) === true) {
|
||||
$model->expires_at = now()->addMinutes(10);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user that the token belongs to.
|
||||
*
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Mail\LoginLink;
|
||||
use App\Traits\UUID;
|
||||
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;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Str;
|
||||
use PharIo\Manifest\Email;
|
||||
|
||||
class User extends Authenticatable implements MustVerifyEmail
|
||||
{
|
||||
@@ -110,34 +105,21 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
});
|
||||
}
|
||||
|
||||
public function createLoginToken($redirect = null)
|
||||
/**
|
||||
* Get the tokens for the user.
|
||||
*
|
||||
* @return HasMany
|
||||
*/
|
||||
public function tokens(): HasMany
|
||||
{
|
||||
// Generate a unique token
|
||||
$token = Str::random(60);
|
||||
|
||||
// Store the token in the database
|
||||
DB::table('login_tokens')->insert([
|
||||
'email' => $this->email,
|
||||
'token' => $token,
|
||||
'intended_url' => $redirect,
|
||||
]);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
public function softDelete()
|
||||
{
|
||||
foreach ($this->fillable as $field) {
|
||||
if ($field === 'email_verified_at') {
|
||||
$this->email_verified_at = null;
|
||||
} else if ($field !== 'email') {
|
||||
$this->{$field} = '';
|
||||
}
|
||||
}
|
||||
|
||||
$this->save();
|
||||
return $this->hasMany(Token::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the calculated name of the user.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
$name = '';
|
||||
@@ -183,14 +165,11 @@ class User extends Authenticatable implements MustVerifyEmail
|
||||
}
|
||||
}
|
||||
|
||||
public function emailUpdate()
|
||||
{
|
||||
return $this->hasOne(EmailUpdate::class);
|
||||
}
|
||||
|
||||
public function getEmailUpdatePendingAttribute()
|
||||
{
|
||||
return $this->emailUpdate()->exists();
|
||||
$emailUpdate = $this->tokens()->where('type', 'email-update')->where('expires_at', '>', now())->first();
|
||||
return $emailUpdate ? $emailUpdate->data['email'] : null;
|
||||
}
|
||||
|
||||
public function isAdmin(): bool
|
||||
|
||||
Reference in New Issue
Block a user