Skip to content

Commit

Permalink
Allow users to disable email registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Dec 1, 2024
1 parent b3c2c94 commit 2a76f9e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/devdojo/auth/descriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'enable_branding' => 'This will toggle on/off the Auth branding at the bottom of each auth screen. Consider leaving on to support and help grow this project.',
'dev_mode' => 'This is for development mode, when set in Dev Mode Assets will be loaded from Vite',
'enable_2fa' => 'Enable the ability for users to turn on Two Factor Authentication',
'enable_email_registration' => 'Enable the ability for users to register via email',
'login_show_social_providers' => 'Show the social providers login buttons on the login form',
'center_align_social_provider_button_content' => 'Center align the content in the social provider button?',
'social_providers_location' => 'The location of the social provider buttons (top or bottom)',
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 @@ -13,6 +13,7 @@
'enable_branding' => true,
'dev_mode' => false,
'enable_2fa' => false, // Enable or disable 2FA functionality globally
'enable_email_registration' => true,
'login_show_social_providers' => true,
'center_align_social_provider_button_content' => false,
'social_providers_location' => 'bottom',
Expand Down
2 changes: 1 addition & 1 deletion resources/views/pages/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function authenticate()
</form>


@if(config('devdojo.auth.settings.registration_enabled'))
@if(config('devdojo.auth.settings.registration_enabled', true))
<div class="mt-3 space-x-0.5 text-sm leading-5 text-left" style="color:{{ config('devdojo.auth.appearance.color.text') }}">
<span class="opacity-[47%]"> {{ config('devdojo.auth.language.login.dont_have_an_account') }} </span>
<x-auth::elements.text-link data-auth="register-link" href="{{ route('auth.register') }}">{{ config('devdojo.auth.language.login.sign_up') }}</x-auth::elements.text-link>
Expand Down
22 changes: 21 additions & 1 deletion resources/views/pages/auth/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
public $showEmailField = true;
public $showPasswordField = false;
public $showPasswordConfirmationField = false;
public $showEmailRegistration = true;
public function rules()
{
if (!$this->settings->enable_email_registration) {
return [];
}
$nameValidationRules = [];
if (config('devdojo.auth.settings.registration_include_name_field')) {
$nameValidationRules = ['name' => 'required'];
Expand All @@ -59,6 +63,15 @@ public function mount()
return;
}
if (!$this->settings->enable_email_registration) {
$this->showEmailRegistration = false;
$this->showNameField = false;
$this->showEmailField = false;
$this->showPasswordField = false;
$this->showPasswordConfirmationField = false;
return;
}
if ($this->settings->registration_include_name_field) {
$this->showNameField = true;
}
Expand All @@ -79,6 +92,11 @@ public function register()
return redirect()->route('auth.login');
}
if (!$this->settings->enable_email_registration) {
session()->flash('error', config('devdojo.auth.language.register.email_registration_disabled', 'Email registration is currently disabled. Please use social login.'));
return;
}
if (!$this->showPasswordField) {
if ($this->settings->registration_include_name_field) {
$this->validateOnly('name');
Expand Down Expand Up @@ -140,6 +158,7 @@ public function register()
<x-auth::elements.social-providers />
@endif

@if($showEmailRegistration)
<form wire:submit="register" class="space-y-5">

@if($showNameField)
Expand All @@ -163,6 +182,7 @@ public function register()

<x-auth::elements.button data-auth="submit-button" rounded="md" submit="true">{{config('devdojo.auth.language.register.button')}}</x-auth::elements.button>
</form>
@endif

<div class="mt-3 space-x-0.5 text-sm leading-5 text-left" style="color:{{ config('devdojo.auth.appearance.color.text') }}">
<span class="opacity-[47%]">{{config('devdojo.auth.language.register.already_have_an_account')}}</span>
Expand Down
50 changes: 50 additions & 0 deletions tests/Feature/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

beforeEach(function () {
config()->set('devdojo.auth.settings.registration_enabled', true);
config()->set('devdojo.auth.settings.enable_email_registration', true);
});

it('allows access to registration page when enabled', function () {
Expand Down Expand Up @@ -43,3 +44,52 @@
expect($component->get('showNameField'))->toBeTrue();
expect($component->get('showPasswordField'))->toBeTrue();
});

it('hides email registration form when email registration is disabled', function () {
config()->set('devdojo.auth.settings.enable_email_registration', false);

$component = Livewire::test('auth.register');

expect($component->get('showEmailRegistration'))->toBeFalse();
expect($component->get('showEmailField'))->toBeFalse();
expect($component->get('showPasswordField'))->toBeFalse();
expect($component->get('showNameField'))->toBeFalse();
});

it('shows email registration form when email registration is enabled', function () {
config()->set('devdojo.auth.settings.enable_email_registration', true);

$component = Livewire::test('auth.register');

expect($component->get('showEmailRegistration'))->toBeTrue();
expect($component->get('showEmailField'))->toBeTrue();
});

it('prevents email registration when disabled', function () {
config()->set('devdojo.auth.settings.enable_email_registration', false);

$component = Livewire::test('auth.register')
->set('email', 'test@example.com')
->set('password', 'password123')
->call('register');

expect(Auth::check())->toBeFalse();
expect(session('error'))->toBe(
config('devdojo.auth.language.register.email_registration_disabled', 'Email registration is currently disabled. Please use social login.')
);
});

it('validates empty rules when email registration is disabled', function () {
config()->set('devdojo.auth.settings.enable_email_registration', false);

$component = Livewire::test('auth.register');

expect($component->instance()->rules())->toBeEmpty();
});

it('preserves social login functionality when email registration is disabled', function () {
config()->set('devdojo.auth.settings.enable_email_registration', false);

Livewire::test('auth.register')
->assertSee('social-providers');
});

0 comments on commit 2a76f9e

Please sign in to comment.