From f52f89735c8951b54d3f9ae3c8288e66b3ef0e73 Mon Sep 17 00:00:00 2001 From: Bobby Iliev Date: Sun, 17 Nov 2024 17:29:44 +0200 Subject: [PATCH] Allow users to disable registrations --- config/devdojo/auth/descriptions.php | 1 + config/devdojo/auth/settings.php | 1 + resources/views/pages/auth/register.blade.php | 13 +++++- tests/Feature/RegistrationTest.php | 45 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/RegistrationTest.php diff --git a/config/devdojo/auth/descriptions.php b/config/devdojo/auth/descriptions.php index 3f1a8fb..2fd362f 100644 --- a/config/devdojo/auth/descriptions.php +++ b/config/devdojo/auth/descriptions.php @@ -6,6 +6,7 @@ return [ 'settings' => [ 'redirect_after_auth' => 'Where should the user be redirected to after they are authenticated?', + 'registration_enabled' => 'Enable or disable registration functionality. If disabled, users will not be able to register for an account.', 'registration_show_password_same_screen' => 'During registrations, show the password on the same screen or show it on an individual screen.', 'registration_include_name_field' => 'During registration, include the Name field.', 'registration_include_password_confirmation_field' => 'During registration, include the Password Confirmation field.', diff --git a/config/devdojo/auth/settings.php b/config/devdojo/auth/settings.php index a088429..cb46d55 100644 --- a/config/devdojo/auth/settings.php +++ b/config/devdojo/auth/settings.php @@ -5,6 +5,7 @@ */ return [ 'redirect_after_auth' => '/', + 'registration_enabled' => true, 'registration_show_password_same_screen' => true, 'registration_include_name_field' => false, 'registration_include_password_confirmation_field' => false, diff --git a/resources/views/pages/auth/register.blade.php b/resources/views/pages/auth/register.blade.php index ad596f8..eb9fe9e 100644 --- a/resources/views/pages/auth/register.blade.php +++ b/resources/views/pages/auth/register.blade.php @@ -53,6 +53,12 @@ public function mount() { $this->loadConfigs(); + if (!$this->settings->registration_enabled) { + session()->flash('error', config('devdojo.auth.language.register.registrations_disabled', 'Registrations are currently disabled.')); + redirect()->route('auth.login'); + return; + } + if ($this->settings->registration_include_name_field) { $this->showNameField = true; } @@ -68,6 +74,11 @@ public function mount() public function register() { + if (!$this->settings->registration_enabled) { + session()->flash('error', config('devdojo.auth.language.register.registrations_disabled', 'Registrations are currently disabled.')); + return redirect()->route('auth.login'); + } + if (!$this->showPasswordField) { if ($this->settings->registration_include_name_field) { $this->validateOnly('name'); @@ -166,4 +177,4 @@ public function register() @endvolt - \ No newline at end of file + diff --git a/tests/Feature/RegistrationTest.php b/tests/Feature/RegistrationTest.php new file mode 100644 index 0000000..8fdc1ff --- /dev/null +++ b/tests/Feature/RegistrationTest.php @@ -0,0 +1,45 @@ +set('devdojo.auth.settings.registration_enabled', true); +}); + +it('allows access to registration page when enabled', function () { + Livewire::test('auth.register') + ->assertOk() + ->assertDontSee('Registrations are currently disabled'); +}); + +it('redirects to login when registrations are disabled', function () { + config()->set('devdojo.auth.settings.registration_enabled', false); + + Livewire::test('auth.register') + ->assertRedirect(route('auth.login')); + + expect(session('error'))->toBe( + config('devdojo.auth.language.register.registrations_disabled', 'Registrations are currently disabled.') + ); +}); + +it('allows registration when enabled', function () { + $component = Livewire::test('auth.register') + ->set('email', 'test@example.com') + ->set('password', 'password123') + ->set('name', 'Test User') + ->call('register'); + + expect(Auth::check())->toBeTrue(); + expect(Auth::user()->email)->toBe('test@example.com'); +}); + +it('preserves other registration settings when enabled', function () { + config()->set('devdojo.auth.settings.registration_include_name_field', true); + config()->set('devdojo.auth.settings.registration_show_password_same_screen', true); + + $component = Livewire::test('auth.register'); + + expect($component->get('showNameField'))->toBeTrue(); + expect($component->get('showPasswordField'))->toBeTrue(); +});