From 46792834207bf0de751efa16b481c88e93b78972 Mon Sep 17 00:00:00 2001 From: Tony Lea Date: Fri, 31 May 2024 21:26:58 -0400 Subject: [PATCH 1/2] Adding latest updates to auth --- config/devdojo/auth/descriptions.php | 1 + config/devdojo/auth/settings.php | 1 + .../elements/social-providers.blade.php | 15 ++++++ resources/views/pages/auth/login.blade.php | 48 ++++++++++++++++++- resources/views/pages/auth/register.blade.php | 12 +---- routes/web.php | 12 +++++ src/AuthServiceProvider.php | 3 +- src/Helper.php | 12 +++++ src/Models/SocialProvider.php | 10 +++- 9 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 resources/views/components/elements/social-providers.blade.php diff --git a/config/devdojo/auth/descriptions.php b/config/devdojo/auth/descriptions.php index 2f95df0..509fab7 100644 --- a/config/devdojo/auth/descriptions.php +++ b/config/devdojo/auth/descriptions.php @@ -12,5 +12,6 @@ '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', + 'login_show_social_providers' => 'Show the social providers login buttons on the login form' ], ]; diff --git a/config/devdojo/auth/settings.php b/config/devdojo/auth/settings.php index 47fc6cd..4482d8b 100644 --- a/config/devdojo/auth/settings.php +++ b/config/devdojo/auth/settings.php @@ -11,4 +11,5 @@ 'enable_branding' => true, 'dev_mode' => false, 'enable_2fa' => false, // Enable or disable 2FA functionality globally + 'login_show_social_providers' => true ]; diff --git a/resources/views/components/elements/social-providers.blade.php b/resources/views/components/elements/social-providers.blade.php new file mode 100644 index 0000000..bce3512 --- /dev/null +++ b/resources/views/components/elements/social-providers.blade.php @@ -0,0 +1,15 @@ +@props([ + 'socialProviders' => \Devdojo\Auth\Helper::activeProviders(), + 'separator' => true, + 'separator_text' => 'or' +]) +@if(count($socialProviders)) + @if($separator) + {{ $separator_text }} + @endif +
+ @foreach($socialProviders as $slug => $provider) + + @endforeach +
+@endif \ No newline at end of file diff --git a/resources/views/pages/auth/login.blade.php b/resources/views/pages/auth/login.blade.php index f8409d4..5a29fb0 100644 --- a/resources/views/pages/auth/login.blade.php +++ b/resources/views/pages/auth/login.blade.php @@ -25,17 +25,28 @@ public $showPasswordField = false; + public $showIdentifierInput = true; + public $showSocialProviderInfo = false; + public $language = []; public $twoFactorEnabled = true; + public $userSocialProviders = []; + public function mount(){ $this->loadConfigs(); $this->twoFactorEnabled = $this->settings->enable_2fa; } public function editIdentity(){ - $this->showPasswordField = false; + if($this->showPasswordField){ + $this->showPasswordField = false; + return; + } + + $this->showIdentifierInput = true; + $this->showSocialProviderInfo = false; } public function authenticate() @@ -43,6 +54,19 @@ public function authenticate() if(!$this->showPasswordField){ $this->validateOnly('email'); + $userTryingToValidate = \Devdojo\Auth\Models\User::where('email', $this->email)->first(); + if(!is_null($userTryingToValidate)){ + if(is_null($userTryingToValidate->password)){ + $this->userSocialProviders = []; + // User is attempting to login and password is null. Need to show Social Provider info + foreach($userTryingToValidate->socialProviders->all() as $provider){ + array_push($this->userSocialProviders, $provider->provider_slug); + } + $this->showIdentifierInput = false; + $this->showSocialProviderInfo = true; + return; + } + } $this->showPasswordField = true; $this->js("setTimeout(function(){ window.dispatchEvent(new CustomEvent('focus-password', {})); }, 10);"); return; @@ -110,7 +134,23 @@ public function authenticate() @else - + @if($showIdentifierInput) + + @endif + @endif + + @if($showSocialProviderInfo) +
+ You have been authenticated via {{ implode(', ', $userSocialProviders) }}. Please login to that network below. + +
+ + @if(!config('devdojo.auth.settings.login_show_social_providers')) + + @endif @endif @if($showPasswordField) @@ -129,6 +169,10 @@ public function authenticate() Sign up + @if(config('devdojo.auth.settings.login_show_social_providers')) + + @endif + @endvolt diff --git a/resources/views/pages/auth/register.blade.php b/resources/views/pages/auth/register.blade.php index dfd1bc3..66034c9 100644 --- a/resources/views/pages/auth/register.blade.php +++ b/resources/views/pages/auth/register.blade.php @@ -30,7 +30,7 @@ public $showPasswordField = false; - public $social_providers = []; + public function rules() { @@ -47,7 +47,6 @@ public function rules() } public function mount(){ - $this->social_providers = Helper::activeProviders(); $this->loadConfigs(); if($this->settings->registration_include_name_field){ @@ -140,14 +139,7 @@ public function register() Sign in - @if(count($this->social_providers)) - or -
- @foreach($this->social_providers as $slug => $provider) - - @endforeach -
- @endif + diff --git a/routes/web.php b/routes/web.php index 76a3760..49cf192 100644 --- a/routes/web.php +++ b/routes/web.php @@ -29,3 +29,15 @@ Route::get('auth/{driver}/redirect', [SocialController::class, 'redirect']); Route::get('auth/{driver}/callback', [SocialController::class, 'callback']); }); + + +Route::get('hey', function(){ + $rowsArray = []; + $socialProviders = config('devdojo.auth.providers', []); + + foreach($socialProviders as $key => $provider){ + $provider['slug'] = $key; + array_push($rowsArray, $provider); + } + dd($rowsArray); +}); \ No newline at end of file diff --git a/src/AuthServiceProvider.php b/src/AuthServiceProvider.php index 0700b35..ed6259b 100644 --- a/src/AuthServiceProvider.php +++ b/src/AuthServiceProvider.php @@ -112,8 +112,9 @@ private function jetstreamFunctionality(){ Config::get('fortify.features', []), [ \Laravel\Fortify\Features::twoFactorAuthentication([ + 'confirm' => true, 'confirmPassword' => true, - ]) + ]), ] )); } diff --git a/src/Helper.php b/src/Helper.php index b781a31..4fedd50 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -18,6 +18,18 @@ public static function activeProviders() return $activeProviders; } + public static function getProvidersFromArray($array){ + $providers = config('devdojo.auth.providers'); + $providersInArray = []; + foreach ($providers as $slug => $provider) { + if ($provider['active'] && in_array($slug, $array)) { + $providersInArray[$slug] = (object) $provider; + } + } + + return $providersInArray; + } + public static function convertSlugToTitle($slug) { $readable = str_replace('_', ' ', str_replace('-', ' ', $slug)); diff --git a/src/Models/SocialProvider.php b/src/Models/SocialProvider.php index 82c3545..c40a185 100644 --- a/src/Models/SocialProvider.php +++ b/src/Models/SocialProvider.php @@ -20,7 +20,15 @@ class SocialProvider extends Model public function getRows() { // Fetching the social providers from the configuration file - $this->rows = config('devdojo.auth.providers', []); + $rowsArray = []; + $socialProviders = config('devdojo.auth.providers', []); + + foreach($socialProviders as $key => $provider){ + $provider['slug'] = $key; + array_push($rowsArray, $provider); + } + + $this->rows = $rowsArray; return $this->rows; } From d9b2e180a2e56edd286ab306e047981c47165b34 Mon Sep 17 00:00:00 2001 From: tnylea Date: Sat, 1 Jun 2024 01:27:30 +0000 Subject: [PATCH 2/2] Fixes coding style --- config/devdojo/auth/descriptions.php | 2 +- config/devdojo/auth/settings.php | 2 +- routes/web.php | 21 ++++++++++----------- src/AuthServiceProvider.php | 9 ++++++--- src/Helper.php | 3 ++- src/Models/SocialProvider.php | 6 +++--- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/config/devdojo/auth/descriptions.php b/config/devdojo/auth/descriptions.php index 509fab7..97b7039 100644 --- a/config/devdojo/auth/descriptions.php +++ b/config/devdojo/auth/descriptions.php @@ -12,6 +12,6 @@ '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', - 'login_show_social_providers' => 'Show the social providers login buttons on the login form' + 'login_show_social_providers' => 'Show the social providers login buttons on the login form', ], ]; diff --git a/config/devdojo/auth/settings.php b/config/devdojo/auth/settings.php index 4482d8b..60bd8f3 100644 --- a/config/devdojo/auth/settings.php +++ b/config/devdojo/auth/settings.php @@ -11,5 +11,5 @@ 'enable_branding' => true, 'dev_mode' => false, 'enable_2fa' => false, // Enable or disable 2FA functionality globally - 'login_show_social_providers' => true + 'login_show_social_providers' => true, ]; diff --git a/routes/web.php b/routes/web.php index 49cf192..f77adf1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -30,14 +30,13 @@ Route::get('auth/{driver}/callback', [SocialController::class, 'callback']); }); - -Route::get('hey', function(){ - $rowsArray = []; - $socialProviders = config('devdojo.auth.providers', []); - - foreach($socialProviders as $key => $provider){ - $provider['slug'] = $key; - array_push($rowsArray, $provider); - } - dd($rowsArray); -}); \ No newline at end of file +Route::get('hey', function () { + $rowsArray = []; + $socialProviders = config('devdojo.auth.providers', []); + + foreach ($socialProviders as $key => $provider) { + $provider['slug'] = $key; + array_push($rowsArray, $provider); + } + dd($rowsArray); +}); diff --git a/src/AuthServiceProvider.php b/src/AuthServiceProvider.php index ed6259b..857b7f0 100644 --- a/src/AuthServiceProvider.php +++ b/src/AuthServiceProvider.php @@ -5,9 +5,9 @@ use Devdojo\Auth\Http\Middleware\TwoFactorChallenged; use Devdojo\Auth\Http\Middleware\TwoFactorEnabled; use Devdojo\Auth\Http\Middleware\ViewAuthSetup; +use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Route; -use Illuminate\Support\Facades\Config; use Illuminate\Support\ServiceProvider; use Laravel\Folio\Folio; use Livewire\Livewire; @@ -101,11 +101,13 @@ private function registerVoltDirectory(): void ]); } - private function handleStarterKitFunctionality(){ + private function handleStarterKitFunctionality() + { $this->jetstreamFunctionality(); } - private function jetstreamFunctionality(){ + private function jetstreamFunctionality() + { // We check if fortify is installed and the user has enabled 2FA, if so we want to enable that feature if (class_exists(\Laravel\Fortify\Features::class) && config('devdojo.auth.settings.enable_2fa')) { Config::set('fortify.features', array_merge( @@ -119,6 +121,7 @@ private function jetstreamFunctionality(){ )); } } + /** * Register the application services. */ diff --git a/src/Helper.php b/src/Helper.php index 4fedd50..b57df0c 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -18,7 +18,8 @@ public static function activeProviders() return $activeProviders; } - public static function getProvidersFromArray($array){ + public static function getProvidersFromArray($array) + { $providers = config('devdojo.auth.providers'); $providersInArray = []; foreach ($providers as $slug => $provider) { diff --git a/src/Models/SocialProvider.php b/src/Models/SocialProvider.php index c40a185..53d9b77 100644 --- a/src/Models/SocialProvider.php +++ b/src/Models/SocialProvider.php @@ -20,10 +20,10 @@ class SocialProvider extends Model public function getRows() { // Fetching the social providers from the configuration file - $rowsArray = []; + $rowsArray = []; $socialProviders = config('devdojo.auth.providers', []); - - foreach($socialProviders as $key => $provider){ + + foreach ($socialProviders as $key => $provider) { $provider['slug'] = $key; array_push($rowsArray, $provider); }