Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to disable registrations #141

Merged
merged 8 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/devdojo/auth/descriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
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.',
'registration_require_email_verification' => 'During registration, require users to verify their email.',
'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?',
'center_align_text' => 'Center align text?',
'social_providers_location' => 'The location of the social provider buttons (top or bottom)',
'check_account_exists_before_login' => 'Determines if the system checks for account existence before login',
],
Expand Down
1 change: 1 addition & 0 deletions config/devdojo/auth/language.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'already_have_an_account' => 'Already have an account?',
'sign_in' => 'Sign in',
'button' => 'Continue',
'email_registration_disabled' => 'Email registration is currently disabled. Please use social login.',
],
'verify' => [
'page_title' => 'Verify Your Account',
Expand Down
3 changes: 3 additions & 0 deletions config/devdojo/auth/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
*/
return [
'redirect_after_auth' => '/',
'registration_enabled' => true,
'registration_show_password_same_screen' => true,
'registration_include_name_field' => false,
'registration_include_password_confirmation_field' => false,
'registration_require_email_verification' => false,
'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,
'center_align_text' => false,
'social_providers_location' => 'bottom',
'check_account_exists_before_login' => false,
];
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@if($separator && config('devdojo.auth.settings.social_providers_location') != 'top')
<x-auth::elements.separator class="my-6">{{ $separator_text }}</x-auth::elements.separator>
@endif
<div class="relative space-y-2 w-full">
<div class="relative space-y-2 w-full @if(config('devdojo.auth.settings.social_providers_location') != 'top' && !$separator){{ 'mt-3' }}@endif">
@foreach($socialProviders as $slug => $provider)
<x-auth::elements.social-button :$slug :$provider />
@endforeach
Expand Down
10 changes: 6 additions & 4 deletions resources/views/pages/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,12 @@ public function authenticate()
</form>


<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>
</div>
@if(config('devdojo.auth.settings.registration_enabled', true))
<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') }}">
<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>
</div>
@endif

@if(config('devdojo.auth.settings.login_show_social_providers') && config('devdojo.auth.settings.social_providers_location') != 'top')
<x-auth::elements.social-providers />
Expand Down
41 changes: 36 additions & 5 deletions 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 @@ -53,6 +57,21 @@ 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->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 @@ -68,6 +87,16 @@ 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->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 redirect()->route('auth.register');
}

if (!$this->showPasswordField) {
if ($this->settings->registration_include_name_field) {
$this->validateOnly('name');
Expand Down Expand Up @@ -126,9 +155,10 @@ public function register()
<x-auth::elements.session-message />

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

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

@if($showNameField)
Expand All @@ -152,18 +182,19 @@ 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') }}">
<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') }}">
<span class="opacity-[47%]">{{config('devdojo.auth.language.register.already_have_an_account')}}</span>
<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>
</div>

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


</x-auth::elements.container>
@endvolt

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

use Illuminate\Support\Facades\Auth;

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

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', '[email protected]')
->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);

config()->set('devdojo.auth.providers', [
'google' => ['name' => 'Google', 'active' => true],
'facebook' => ['name' => 'Facebook', 'active' => false],
]);

Livewire::test('auth.register')
->assertSee('Google');
});
Loading