Feat: Moving things, updating OTP, cleaning stuff up

This commit is contained in:
2026-07-03 02:15:31 +03:00
parent 15bff15cad
commit ce08287641
23 changed files with 770 additions and 119 deletions
+12
View File
@@ -0,0 +1,12 @@
<?php
namespace Modules\Core\Auth\Events;
use Illuminate\Contracts\Auth\Authenticatable;
class UserCreated
{
public function __construct(
public readonly Authenticatable $user,
) {}
}
@@ -6,7 +6,7 @@ use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
class CustomerOtpMail extends Mailable
class UserOtpMail extends Mailable
{
public function __construct(
public readonly string $name,
@@ -20,6 +20,6 @@ class CustomerOtpMail extends Mailable
public function content(): Content
{
return new Content(view: 'core::auth.mail.customer-otp');
return new Content(view: 'core::auth.mail.user-otp');
}
}
-61
View File
@@ -1,61 +0,0 @@
<?php
namespace Modules\Core\Auth\Services;
use Illuminate\Support\Facades\Mail;
use Lunar\Models\Contracts\Customer;
use Lunar\Facades\ModelManifest;
use Modules\Core\Auth\Mail\CustomerOtpMail;
class CustomerOtpService
{
private const EXPIRY_MINUTES = 10;
private const CODE_LENGTH = 6;
public function generateAndSend(string $email): bool
{
$customer = $this->findByEmail($email);
if (! $customer) {
return false;
}
$code = str_pad((string) random_int(0, 999999), self::CODE_LENGTH, '0', STR_PAD_LEFT);
$customer->otp_code = $code;
$customer->otp_expires_at = now()->addMinutes(self::EXPIRY_MINUTES);
$customer->save();
Mail::to($email)->send(new CustomerOtpMail($customer->full_name, $code));
return true;
}
public function validate(string $email, string $code): ?Customer
{
$customer = $this->findByEmail($email);
if (! $customer) {
return null;
}
if (! $customer->otp_expires_at || $customer->otp_code != $code || now()->isAfter($customer->otp_expires_at)) {
return null;
}
$customer->otp_code = null;
$customer->otp_expires_at = null;
$customer->save();
return $customer;
}
private function findByEmail(string $email): ?Customer
{
$model = ModelManifest::get(Customer::class);
return $model::query()
->whereHas('emails', fn ($query) => $query->where('email', $email))
->first();
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace Modules\Core\Auth\Services;
use Illuminate\Support\Facades\Mail;
use Modules\Core\Auth\Mail\UserOtpMail;
class UserOtpService
{
private const EXPIRY_MINUTES = 10;
private const CODE_LENGTH = 6;
public function generateAndSend(string $email): bool
{
$model = config('auth.providers.users.model');
$user = $model::firstOrCreate(['email' => $email]);
$code = str_pad((string) random_int(0, 999999), self::CODE_LENGTH, '0', STR_PAD_LEFT);
$user->otp_code = $code;
$user->otp_expires_at = now()->addMinutes(self::EXPIRY_MINUTES);
$user->save();
Mail::to($user->email)->send(new UserOtpMail($user->name ?? $user->email, $code));
return true;
}
public function validate(string $email, string $code)
{
$model = config('auth.providers.users.model');
$user = $model::where('email', $email)->first();
if (! $user) {
return null;
}
if (! $user->otp_expires_at || $user->otp_code != $code || now()->isAfter($user->otp_expires_at)) {
return null;
}
$user->otp_code = null;
$user->otp_expires_at = null;
$user->save();
return $user;
}
}