Skip to content

Commit

Permalink
Allow users to disable registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Nov 17, 2024
1 parent 224800f commit f52f897
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/devdojo/auth/descriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
1 change: 1 addition & 0 deletions config/devdojo/auth/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion resources/views/pages/auth/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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');
Expand Down Expand Up @@ -166,4 +177,4 @@ public function register()
</x-auth::elements.container>
@endvolt

</x-auth::layouts.app>
</x-auth::layouts.app>
45 changes: 45 additions & 0 deletions tests/Feature/RegistrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Illuminate\Support\Facades\Auth;

beforeEach(function () {
config()->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', '[email protected]')
->set('password', 'password123')
->set('name', 'Test User')
->call('register');

expect(Auth::check())->toBeTrue();
expect(Auth::user()->email)->toBe('[email protected]');
});

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();
});

0 comments on commit f52f897

Please sign in to comment.