Skip to content

Commit 1b04d44

Browse files
authored
Merge pull request #141 from thedevdojo/allow-registration-disabling
Allow users to disable registrations
2 parents e020161 + f6b7136 commit 1b04d44

File tree

7 files changed

+149
-9
lines changed

7 files changed

+149
-9
lines changed

config/devdojo/auth/descriptions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
return [
77
'settings' => [
88
'redirect_after_auth' => 'Where should the user be redirected to after they are authenticated?',
9+
'registration_enabled' => 'Enable or disable registration functionality. If disabled, users will not be able to register for an account.',
910
'registration_show_password_same_screen' => 'During registrations, show the password on the same screen or show it on an individual screen.',
1011
'registration_include_name_field' => 'During registration, include the Name field.',
1112
'registration_include_password_confirmation_field' => 'During registration, include the Password Confirmation field.',
1213
'registration_require_email_verification' => 'During registration, require users to verify their email.',
1314
'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.',
1415
'dev_mode' => 'This is for development mode, when set in Dev Mode Assets will be loaded from Vite',
1516
'enable_2fa' => 'Enable the ability for users to turn on Two Factor Authentication',
17+
'enable_email_registration' => 'Enable the ability for users to register via email',
1618
'login_show_social_providers' => 'Show the social providers login buttons on the login form',
1719
'center_align_social_provider_button_content' => 'Center align the content in the social provider button?',
20+
'center_align_text' => 'Center align text?',
1821
'social_providers_location' => 'The location of the social provider buttons (top or bottom)',
1922
'check_account_exists_before_login' => 'Determines if the system checks for account existence before login',
2023
],

config/devdojo/auth/language.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'already_have_an_account' => 'Already have an account?',
3434
'sign_in' => 'Sign in',
3535
'button' => 'Continue',
36+
'email_registration_disabled' => 'Email registration is currently disabled. Please use social login.',
3637
],
3738
'verify' => [
3839
'page_title' => 'Verify Your Account',

config/devdojo/auth/settings.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
*/
66
return [
77
'redirect_after_auth' => '/',
8+
'registration_enabled' => true,
89
'registration_show_password_same_screen' => true,
910
'registration_include_name_field' => false,
1011
'registration_include_password_confirmation_field' => false,
1112
'registration_require_email_verification' => false,
1213
'enable_branding' => true,
1314
'dev_mode' => false,
1415
'enable_2fa' => false, // Enable or disable 2FA functionality globally
16+
'enable_email_registration' => true,
1517
'login_show_social_providers' => true,
1618
'center_align_social_provider_button_content' => false,
19+
'center_align_text' => false,
1720
'social_providers_location' => 'bottom',
1821
'check_account_exists_before_login' => false,
1922
];

resources/views/components/elements/social-providers.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@if($separator && config('devdojo.auth.settings.social_providers_location') != 'top')
88
<x-auth::elements.separator class="my-6">{{ $separator_text }}</x-auth::elements.separator>
99
@endif
10-
<div class="relative space-y-2 w-full">
10+
<div class="relative space-y-2 w-full @if(config('devdojo.auth.settings.social_providers_location') != 'top' && !$separator){{ 'mt-3' }}@endif">
1111
@foreach($socialProviders as $slug => $provider)
1212
<x-auth::elements.social-button :$slug :$provider />
1313
@endforeach

resources/views/pages/auth/login.blade.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ public function authenticate()
192192
</form>
193193

194194

195-
<div class="mt-3 space-x-0.5 text-sm leading-5 text-left" style="color:{{ config('devdojo.auth.appearance.color.text') }}">
196-
<span class="opacity-[47%]"> {{ config('devdojo.auth.language.login.dont_have_an_account') }} </span>
197-
<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>
198-
</div>
195+
@if(config('devdojo.auth.settings.registration_enabled', true))
196+
<div class="mt-3 space-x-0.5 text-sm leading-5 @if(config('devdojo.auth.settings.center_align_text')){{ 'text-center' }}@else{{ 'text-left' }}@endif" style="color:{{ config('devdojo.auth.appearance.color.text') }}">
197+
<span class="opacity-[47%]"> {{ config('devdojo.auth.language.login.dont_have_an_account') }} </span>
198+
<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>
199+
</div>
200+
@endif
199201

200202
@if(config('devdojo.auth.settings.login_show_social_providers') && config('devdojo.auth.settings.social_providers_location') != 'top')
201203
<x-auth::elements.social-providers />

resources/views/pages/auth/register.blade.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929
public $showEmailField = true;
3030
public $showPasswordField = false;
3131
public $showPasswordConfirmationField = false;
32-
32+
public $showEmailRegistration = true;
3333
3434
public function rules()
3535
{
36+
if (!$this->settings->enable_email_registration) {
37+
return [];
38+
}
39+
3640
$nameValidationRules = [];
3741
if (config('devdojo.auth.settings.registration_include_name_field')) {
3842
$nameValidationRules = ['name' => 'required'];
@@ -53,6 +57,21 @@ public function mount()
5357
{
5458
$this->loadConfigs();
5559
60+
if (!$this->settings->registration_enabled) {
61+
session()->flash('error', config('devdojo.auth.language.register.registrations_disabled', 'Registrations are currently disabled.'));
62+
redirect()->route('auth.login');
63+
return;
64+
}
65+
66+
if (!$this->settings->enable_email_registration) {
67+
$this->showEmailRegistration = false;
68+
$this->showNameField = false;
69+
$this->showEmailField = false;
70+
$this->showPasswordField = false;
71+
$this->showPasswordConfirmationField = false;
72+
return;
73+
}
74+
5675
if ($this->settings->registration_include_name_field) {
5776
$this->showNameField = true;
5877
}
@@ -68,6 +87,16 @@ public function mount()
6887
6988
public function register()
7089
{
90+
if (!$this->settings->registration_enabled) {
91+
session()->flash('error', config('devdojo.auth.language.register.registrations_disabled', 'Registrations are currently disabled.'));
92+
return redirect()->route('auth.login');
93+
}
94+
95+
if (!$this->settings->enable_email_registration) {
96+
session()->flash('error', config('devdojo.auth.language.register.email_registration_disabled', 'Email registration is currently disabled. Please use social login.'));
97+
return redirect()->route('auth.register');
98+
}
99+
71100
if (!$this->showPasswordField) {
72101
if ($this->settings->registration_include_name_field) {
73102
$this->validateOnly('name');
@@ -126,9 +155,10 @@ public function register()
126155
<x-auth::elements.session-message />
127156

128157
@if(config('devdojo.auth.settings.social_providers_location') == 'top')
129-
<x-auth::elements.social-providers />
158+
<x-auth::elements.social-providers :separator="$showEmailRegistration" />
130159
@endif
131160

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

134164
@if($showNameField)
@@ -152,14 +182,15 @@ public function register()
152182

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

156-
<div class="mt-3 space-x-0.5 text-sm leading-5 text-left" style="color:{{ config('devdojo.auth.appearance.color.text') }}">
187+
<div class="@if(config('devdojo.auth.settings.social_providers_location') != 'top' && $showEmailRegistration){{ 'mt-3' }}@endif space-x-0.5 text-sm leading-5 @if(config('devdojo.auth.settings.center_align_text')){{ 'text-center' }}@else{{ 'text-left' }}@endif" style="color:{{ config('devdojo.auth.appearance.color.text') }}">
157188
<span class="opacity-[47%]">{{config('devdojo.auth.language.register.already_have_an_account')}}</span>
158189
<x-auth::elements.text-link data-auth="login-link" href="{{ route('auth.login') }}">{{config('devdojo.auth.language.register.sign_in')}}</x-auth::elements.text-link>
159190
</div>
160191

161192
@if(config('devdojo.auth.settings.social_providers_location') != 'top')
162-
<x-auth::elements.social-providers />
193+
<x-auth::elements.social-providers :separator="$showEmailRegistration" />
163194
@endif
164195

165196

tests/Feature/RegistrationTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Auth;
4+
5+
beforeEach(function () {
6+
config()->set('devdojo.auth.settings.registration_enabled', true);
7+
config()->set('devdojo.auth.settings.enable_email_registration', true);
8+
});
9+
10+
it('allows access to registration page when enabled', function () {
11+
Livewire::test('auth.register')
12+
->assertOk()
13+
->assertDontSee('Registrations are currently disabled');
14+
});
15+
16+
it('redirects to login when registrations are disabled', function () {
17+
config()->set('devdojo.auth.settings.registration_enabled', false);
18+
19+
Livewire::test('auth.register')
20+
->assertRedirect(route('auth.login'));
21+
22+
expect(session('error'))->toBe(
23+
config('devdojo.auth.language.register.registrations_disabled', 'Registrations are currently disabled.')
24+
);
25+
});
26+
27+
it('allows registration when enabled', function () {
28+
$component = Livewire::test('auth.register')
29+
->set('email', 'test@example.com')
30+
->set('password', 'password123')
31+
->set('name', 'Test User')
32+
->call('register');
33+
34+
expect(Auth::check())->toBeTrue();
35+
expect(Auth::user()->email)->toBe('test@example.com');
36+
});
37+
38+
it('preserves other registration settings when enabled', function () {
39+
config()->set('devdojo.auth.settings.registration_include_name_field', true);
40+
config()->set('devdojo.auth.settings.registration_show_password_same_screen', true);
41+
42+
$component = Livewire::test('auth.register');
43+
44+
expect($component->get('showNameField'))->toBeTrue();
45+
expect($component->get('showPasswordField'))->toBeTrue();
46+
});
47+
48+
it('hides email registration form when email registration is disabled', function () {
49+
config()->set('devdojo.auth.settings.enable_email_registration', false);
50+
51+
$component = Livewire::test('auth.register');
52+
53+
expect($component->get('showEmailRegistration'))->toBeFalse();
54+
expect($component->get('showEmailField'))->toBeFalse();
55+
expect($component->get('showPasswordField'))->toBeFalse();
56+
expect($component->get('showNameField'))->toBeFalse();
57+
});
58+
59+
it('shows email registration form when email registration is enabled', function () {
60+
config()->set('devdojo.auth.settings.enable_email_registration', true);
61+
62+
$component = Livewire::test('auth.register');
63+
64+
expect($component->get('showEmailRegistration'))->toBeTrue();
65+
expect($component->get('showEmailField'))->toBeTrue();
66+
});
67+
68+
it('prevents email registration when disabled', function () {
69+
config()->set('devdojo.auth.settings.enable_email_registration', false);
70+
71+
$component = Livewire::test('auth.register')
72+
->set('email', 'test@example.com')
73+
->set('password', 'password123')
74+
->call('register');
75+
76+
expect(Auth::check())->toBeFalse();
77+
expect(session('error'))->toBe(
78+
config('devdojo.auth.language.register.email_registration_disabled', 'Email registration is currently disabled. Please use social login.')
79+
);
80+
});
81+
82+
it('validates empty rules when email registration is disabled', function () {
83+
config()->set('devdojo.auth.settings.enable_email_registration', false);
84+
85+
$component = Livewire::test('auth.register');
86+
87+
expect($component->instance()->rules())->toBeEmpty();
88+
});
89+
90+
it('preserves social login functionality when email registration is disabled', function () {
91+
config()->set('devdojo.auth.settings.enable_email_registration', false);
92+
93+
config()->set('devdojo.auth.providers', [
94+
'google' => ['name' => 'Google', 'active' => true],
95+
'facebook' => ['name' => 'Facebook', 'active' => false],
96+
]);
97+
98+
Livewire::test('auth.register')
99+
->assertSee('Google');
100+
});

0 commit comments

Comments
 (0)