From c1a1681cd9eecf2e0fff8500dffbc358e604041e Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Mon, 25 Dec 2023 07:11:45 +0700 Subject: [PATCH 1/2] feat(db): initialize `user_devices` table Signed-off-by: Fery Wardiyanto --- ...ate_identities_address_and_files_table.php | 6 +++++ src/Contracts/HasDevices.php | 16 ++++++++++++ src/Models/Concerns/WithDevices.php | 19 ++++++++++++++ src/Models/UserDevice.php | 26 +++++++++++++++++++ tests/Fixtures/User.php | 2 ++ 5 files changed, 69 insertions(+) create mode 100644 src/Contracts/HasDevices.php create mode 100644 src/Models/Concerns/WithDevices.php create mode 100644 src/Models/UserDevice.php diff --git a/database/migrations/2022_05_10_000001_create_identities_address_and_files_table.php b/database/migrations/2022_05_10_000001_create_identities_address_and_files_table.php index 885038c..f5a3305 100644 --- a/database/migrations/2022_05_10_000001_create_identities_address_and_files_table.php +++ b/database/migrations/2022_05_10_000001_create_identities_address_and_files_table.php @@ -11,6 +11,12 @@ */ public function up(): void { + Schema::create('user_devices', function (Blueprint $table) { + $table->id(); + $table->foreignId('user_id')->constrained()->cascadeOnDelete(); + $table->string('token'); + }); + Schema::create('profiles', function (Blueprint $table) { $table->id(); $table->nullableMorphs('identity'); diff --git a/src/Contracts/HasDevices.php b/src/Contracts/HasDevices.php new file mode 100644 index 0000000..5a5e0eb --- /dev/null +++ b/src/Contracts/HasDevices.php @@ -0,0 +1,16 @@ + $devices + * + * @mixin \Illuminate\Database\Eloquent\Model + */ +interface HasDevices +{ + /** + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function devices(); +} diff --git a/src/Models/Concerns/WithDevices.php b/src/Models/Concerns/WithDevices.php new file mode 100644 index 0000000..8f55208 --- /dev/null +++ b/src/Models/Concerns/WithDevices.php @@ -0,0 +1,19 @@ +hasMany(UserDevice::class); + } +} diff --git a/src/Models/UserDevice.php b/src/Models/UserDevice.php new file mode 100644 index 0000000..487282b --- /dev/null +++ b/src/Models/UserDevice.php @@ -0,0 +1,26 @@ +belongsTo('\App\Models\User'); + } +} diff --git a/tests/Fixtures/User.php b/tests/Fixtures/User.php index 2d4125e..baa1932 100644 --- a/tests/Fixtures/User.php +++ b/tests/Fixtures/User.php @@ -3,6 +3,7 @@ namespace Creasi\Tests\Fixtures; use Creasi\Base\Contracts\HasIdentity; +use Creasi\Base\Models\Concerns\WithDevices; use Creasi\Base\Models\Concerns\WithIdentity; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -25,6 +26,7 @@ class User extends Authenticatable implements HasIdentity use HasApiTokens; use HasFactory; use Notifiable; + use WithDevices; use WithIdentity; protected static function newFactory() From bd30ac94fe25d7fe3a897a43d0cd55d61c30ee45 Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Mon, 25 Dec 2023 07:18:36 +0700 Subject: [PATCH 2/2] chore: add listener to register device on user authenticated Signed-off-by: Fery Wardiyanto --- src/Events/UserDeviceRegistered.php | 14 ++++++++++++ src/Listeners/RegisterUserDevice.php | 32 ++++++++++++++++++++++++++++ src/ServiceProvider.php | 6 ++++++ 3 files changed, 52 insertions(+) create mode 100644 src/Events/UserDeviceRegistered.php create mode 100644 src/Listeners/RegisterUserDevice.php diff --git a/src/Events/UserDeviceRegistered.php b/src/Events/UserDeviceRegistered.php new file mode 100644 index 0000000..9feec00 --- /dev/null +++ b/src/Events/UserDeviceRegistered.php @@ -0,0 +1,14 @@ +request->filled('device')) { + return; + } + + /** @var \Creasi\Base\Contracts\HasDevices */ + $user = $event->user; + + $device = $user->devices()->firstOrCreate([ + 'device' => $this->request->input('device'), + ]); + + \event(new UserDeviceRegistered($device)); + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index dfde052..121e72e 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -9,7 +9,9 @@ use Creasi\Base\Models\Entity; use Creasi\Base\Models\Enums\BusinessRelativeType; use Creasi\Base\View\Composers\TranslationsComposer; +use Illuminate\Auth\Events\Authenticated; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\View; @@ -64,6 +66,10 @@ public function register(): void } $this->registerBindings(); + + $this->booting(function (): void { + Event::listen(Authenticated::class, Listeners\RegisterUserDevice::class); + }); } protected function registerPublishables(): void