Skip to content

Commit

Permalink
Merge pull request #27 from thedevdojo/jetstreamMods
Browse files Browse the repository at this point in the history
Adding latest updates to auth
  • Loading branch information
tnylea authored Jun 1, 2024
2 parents 518e69e + d9b2e18 commit c9ccff5
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 13 deletions.
1 change: 1 addition & 0 deletions config/devdojo/auth/descriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];
1 change: 1 addition & 0 deletions config/devdojo/auth/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
'enable_branding' => true,
'dev_mode' => false,
'enable_2fa' => false, // Enable or disable 2FA functionality globally
'login_show_social_providers' => true,
];
15 changes: 15 additions & 0 deletions resources/views/components/elements/social-providers.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@props([
'socialProviders' => \Devdojo\Auth\Helper::activeProviders(),
'separator' => true,
'separator_text' => 'or'
])
@if(count($socialProviders))
@if($separator)
<x-auth::elements.separator class="my-7">{{ $separator_text }}</x-auto::elements.separator>
@endif
<div class="relative space-y-2 w-full">
@foreach($socialProviders as $slug => $provider)
<x-auth::elements.social-button :$slug :$provider />
@endforeach
</div>
@endif
48 changes: 46 additions & 2 deletions resources/views/pages/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,48 @@
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()
{
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;
Expand Down Expand Up @@ -110,7 +134,23 @@ public function authenticate()
<button type="button" wire:click="editIdentity" class="font-medium text-blue-500">Edit</button>
</x-auth::elements.input-placeholder>
@else
<x-auth::elements.input label="Email Address" type="email" wire:model="email" autofocus="true" id="email" required />
@if($showIdentifierInput)
<x-auth::elements.input label="Email Address" type="email" wire:model="email" autofocus="true" id="email" required />
@endif
@endif

@if($showSocialProviderInfo)
<div class="p-4 text-sm rounded-md border bg-zinc-50 border-zinc-200">
<span>You have been authenticated via {{ implode(', ', $userSocialProviders) }}. Please login to that network below.</span>
<button wire:click="editIdentity" type="button" class="underline translate-x-1.5">Change Email</button>
</div>

@if(!config('devdojo.auth.settings.login_show_social_providers'))
<x-auth::elements.social-providers
:socialProviders="\Devdojo\Auth\Helper::getProvidersFromArray($userSocialProviders)"
:separator="false"
/>
@endif
@endif

@if($showPasswordField)
Expand All @@ -129,6 +169,10 @@ public function authenticate()
<x-auth::elements.text-link href="{{ route('auth.register') }}">Sign up</x-auth::elements.text-link>
</div>

@if(config('devdojo.auth.settings.login_show_social_providers'))
<x-auth::elements.social-providers />
@endif

</x-auth::elements.container>
</div>
@endvolt
Expand Down
12 changes: 2 additions & 10 deletions resources/views/pages/auth/register.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public $showPasswordField = false;
public $social_providers = [];
public function rules()
{
Expand All @@ -47,7 +47,6 @@ public function rules()
}
public function mount(){
$this->social_providers = Helper::activeProviders();
$this->loadConfigs();
if($this->settings->registration_include_name_field){
Expand Down Expand Up @@ -140,14 +139,7 @@ public function register()
<x-auth::elements.text-link href="{{ route('auth.login') }}">Sign in</x-auth::elements.text-link>
</div>

@if(count($this->social_providers))
<x-auth::elements.separator class="my-7">or</x-auto::elements.separator>
<div class="relative space-y-2 w-full">
@foreach($this->social_providers as $slug => $provider)
<x-auth::elements.social-button :$slug :$provider />
@endforeach
</div>
@endif
<x-auth::elements.social-providers />


</x-auth::elements.container>
Expand Down
11 changes: 11 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@
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);
});
1 change: 1 addition & 0 deletions src/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private function jetstreamFunctionality()
Config::get('fortify.features', []),
[
\Laravel\Fortify\Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]),
]
Expand Down
13 changes: 13 additions & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ 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));
Expand Down
10 changes: 9 additions & 1 deletion src/Models/SocialProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit c9ccff5

Please sign in to comment.