From 73d7747fb0eb1caecc67359d5e48823b7c782230 Mon Sep 17 00:00:00 2001 From: Arthur Monney Date: Fri, 19 Jan 2024 21:00:26 +0100 Subject: [PATCH] Add prompt to create admin user (#231) * Add prompt to create admin user * fix code formatting --------- Co-authored-by: mckenziearts --- composer.json | 2 +- composer.lock | 2 +- packages/admin/composer.json | 1 + packages/admin/src/Console/UserCommand.php | 46 ++++++++++++++++------ 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 67b388697..dc64d597e 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,10 @@ "blade-ui-kit/blade-heroicons": "^1.2", "danharrin/livewire-rate-limiting": "^0.3|^1.0", "doctrine/dbal": "^3.6", + "larastan/larastan": "^2.0", "laravel/pint": "^1.1", "mockery/mockery": "^1.4", "nunomaduro/collision": "^5.11|^6.1", - "larastan/larastan": "^2.0", "orchestra/testbench": "^7.0|^8.0", "pestphp/pest": "^1.21", "pestphp/pest-plugin-laravel": "^1.0", diff --git a/composer.lock b/composer.lock index 57dbcfccd..48ae3c1b2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8585ae050bf14065db57779174706bd1", + "content-hash": "59658a19412536d3fa229e89a4bcbb0d", "packages": [], "packages-dev": [ { diff --git a/packages/admin/composer.json b/packages/admin/composer.json index 5801eb1a2..5e1e92678 100644 --- a/packages/admin/composer.json +++ b/packages/admin/composer.json @@ -32,6 +32,7 @@ "illuminate/view": "^9.0|^10.0", "jenssegers/agent": "^2.6", "laravel/helpers": "^1.4.1", + "laravel/prompts": "^0.1.15", "livewire/livewire": "^2.10", "maatwebsite/excel": "^3.1", "mckenziearts/blade-untitledui-icons": "^1.2", diff --git a/packages/admin/src/Console/UserCommand.php b/packages/admin/src/Console/UserCommand.php index fad5a5958..9ba6dde80 100644 --- a/packages/admin/src/Console/UserCommand.php +++ b/packages/admin/src/Console/UserCommand.php @@ -10,6 +10,10 @@ use Illuminate\Support\Facades\Hash; use Shopper\Core\Models\User; +use function Laravel\Prompts\info; +use function Laravel\Prompts\password; +use function Laravel\Prompts\text; + final class UserCommand extends Command { protected $signature = 'shopper:admin'; @@ -18,24 +22,42 @@ final class UserCommand extends Command public function handle(): void { - $this->info('Create Admin User for Shopper administration panel'); + info('Create Admin User for Shopper administration panel'); $this->createUser(); - $this->info('User created successfully.'); + info('User created successfully.'); } protected function createUser(): void { - $email = $this->ask('Email Address', 'admin@shopper.dev'); - $first_name = $this->ask('First Name', 'Shopper'); - $last_name = $this->ask('Last Name', 'User'); - $password = $this->secret('Password'); - $confirmPassword = $this->secret('Confirm Password'); - - if ($password !== $confirmPassword) { - $this->info('Passwords don\'t match'); - } + $email = text( + label: 'Your Email Address', + default: 'admin@shopper.dev', + required: true, + validate: fn (string $value) => User::where('email', $value)->exists() + ? 'An admin with that email already exists.' + : null, + ); + $first_name = text( + label: 'What is your First Name', + default: 'Shopper', + required: true, + ); + $last_name = text( + label: 'What is your Last Name', + default: 'User', + required: true, + ); + $password = password( + label: 'Choose a Password', + required: true, + validate: fn (string $value) => match (true) { + mb_strlen($value) < 6 => 'The password must be at least 6 characters.', + default => null + }, + hint: 'Minimum 6 characters.' + ); - $this->info('Creating admin account...'); + info('Creating admin account...'); $userData = [ 'email' => $email,