From baa28e0d1456f88dacec472e212b5db681b7d785 Mon Sep 17 00:00:00 2001 From: Joel Butcher Date: Wed, 1 Nov 2023 11:19:41 +0000 Subject: [PATCH] [5.x] Align breeze livewire stacks (#311) * bump min breeze version * refactor livewire class stack * refactor livewire functional stack --- composer.json | 2 +- src/Installer/Drivers/Breeze/BreezeDriver.php | 2 +- .../views/livewire/pages/auth/login.blade.php | 50 +++------------- .../livewire/pages/auth/register.blade.php | 5 +- .../profile/connected-accounts-form.blade.php | 8 ++- .../profile/delete-user-form.blade.php | 10 ++-- .../profile/set-password-form.blade.php | 5 +- .../views/livewire/pages/auth/login.blade.php | 59 ++++--------------- .../livewire/pages/auth/register.blade.php | 6 +- .../profile/connected-accounts-form.blade.php | 17 ++++-- .../profile/delete-user-form.blade.php | 21 ++++--- .../profile/set-password-form.blade.php | 8 ++- 12 files changed, 73 insertions(+), 120 deletions(-) diff --git a/composer.json b/composer.json index c37e8f05..58d5a85c 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "webmozart/assert": "^1.11" }, "require-dev": { - "laravel/breeze": "^1.25", + "laravel/breeze": "^1.26", "laravel/jetstream": "^4.0", "laravel/sanctum": "^3.3", "livewire/livewire": "^3", diff --git a/src/Installer/Drivers/Breeze/BreezeDriver.php b/src/Installer/Drivers/Breeze/BreezeDriver.php index 42ed29ad..4dd6cd0a 100644 --- a/src/Installer/Drivers/Breeze/BreezeDriver.php +++ b/src/Installer/Drivers/Breeze/BreezeDriver.php @@ -32,7 +32,7 @@ class_exists('App\Http\Middleware\HandleInertiaRequests') spin(function () use ($options, $composerBinary) { if (! $this->hasComposerPackage('laravel/breeze')) { - $this->requireComposerPackages(['laravel/breeze'], $composerBinary); + $this->requireComposerPackages(['laravel/breeze:^1.26'], $composerBinary); } (new Process([ diff --git a/stubs/breeze/livewire-functional/resources/views/livewire/pages/auth/login.blade.php b/stubs/breeze/livewire-functional/resources/views/livewire/pages/auth/login.blade.php index be6c2b8b..ca302f56 100644 --- a/stubs/breeze/livewire-functional/resources/views/livewire/pages/auth/login.blade.php +++ b/stubs/breeze/livewire-functional/resources/views/livewire/pages/auth/login.blade.php @@ -1,54 +1,22 @@ '', 'password' => '', 'remember' => false]); - -rules([ - 'email' => ['required', 'string', 'email'], - 'password' => ['required', 'string'], - 'remember' => ['boolean'], -]); +form(LoginForm::class); $login = function () { $this->validate(); - $throttleKey = Str::transliterate(Str::lower($this->email).'|'.request()->ip()); - - if (RateLimiter::tooManyAttempts($throttleKey, 5)) { - event(new Lockout(request())); - - $seconds = RateLimiter::availableIn($throttleKey); - - throw ValidationException::withMessages([ - 'email' => trans('auth.throttle', [ - 'seconds' => $seconds, - 'minutes' => ceil($seconds / 60), - ]), - ]); - } - - if (! auth()->attempt($this->only(['email', 'password'], $this->remember))) { - RateLimiter::hit($throttleKey); - - throw ValidationException::withMessages([ - 'email' => trans('auth.failed'), - ]); - } - - RateLimiter::clear($throttleKey); + $this->form->authenticate(); - session()->regenerate(); + Session::regenerate(); $this->redirect( session('url.intended', RouteServiceProvider::HOME), @@ -66,7 +34,7 @@
- +
@@ -74,7 +42,7 @@
- @@ -85,7 +53,7 @@
diff --git a/stubs/breeze/livewire-functional/resources/views/livewire/pages/auth/register.blade.php b/stubs/breeze/livewire-functional/resources/views/livewire/pages/auth/register.blade.php index a27a7563..4d396928 100644 --- a/stubs/breeze/livewire-functional/resources/views/livewire/pages/auth/register.blade.php +++ b/stubs/breeze/livewire-functional/resources/views/livewire/pages/auth/register.blade.php @@ -3,6 +3,7 @@ use App\Models\User; use App\Providers\RouteServiceProvider; use Illuminate\Auth\Events\Registered; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rules; @@ -21,7 +22,7 @@ rules([ 'name' => ['required', 'string', 'max:255'], - 'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class], + 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class], 'password' => ['required', 'string', 'confirmed', Rules\Password::defaults()], ]); @@ -32,7 +33,7 @@ event(new Registered($user = User::create($validated))); - auth()->login($user); + Auth::login($user); $this->redirect(RouteServiceProvider::HOME, navigate: true); }; diff --git a/stubs/breeze/livewire-functional/resources/views/livewire/profile/connected-accounts-form.blade.php b/stubs/breeze/livewire-functional/resources/views/livewire/profile/connected-accounts-form.blade.php index 263ebf2d..0890b87e 100644 --- a/stubs/breeze/livewire-functional/resources/views/livewire/profile/connected-accounts-form.blade.php +++ b/stubs/breeze/livewire-functional/resources/views/livewire/profile/connected-accounts-form.blade.php @@ -1,5 +1,7 @@ validate(); - auth()->user()->connectedAccounts() + Auth::user()->connectedAccounts() ->where('id', $id) ->delete(); @@ -36,14 +38,14 @@ @foreach (JoelButcher\Socialstream\Socialstream::providers() as $provider) @php $account = null; - $account = auth()->user()->connectedAccounts->where('provider', $provider['id'])->first(); + $account = Auth::user()->connectedAccounts->where('provider', $provider['id'])->first(); @endphp @if (! is_null($account))
- @if ((auth()->user()->connectedAccounts->count() > 1 || ! is_null(auth()->user()->getAuthPassword()))) + @if ((Auth::user()->connectedAccounts->count() > 1 || ! is_null(Auth::user()->getAuthPassword()))) ['required', 'string', 'current_password']]); -$deleteUser = function () { +$deleteUser = function (Logout $logout) { $this->validate(); - $user = tap(auth()->user(), fn () => auth()->logout()); + $user = tap(Auth::user(), $logout(...)); $user->connectedAccounts->each->delete(); $user->delete(); - session()->invalidate(); - session()->regenerateToken(); - $this->redirect('/', navigate: true); }; diff --git a/stubs/breeze/livewire-functional/resources/views/livewire/profile/set-password-form.blade.php b/stubs/breeze/livewire-functional/resources/views/livewire/profile/set-password-form.blade.php index bac99c66..ecd9c75a 100644 --- a/stubs/breeze/livewire-functional/resources/views/livewire/profile/set-password-form.blade.php +++ b/stubs/breeze/livewire-functional/resources/views/livewire/profile/set-password-form.blade.php @@ -1,6 +1,7 @@ validate(); } catch (ValidationException $e) { - $this->reset('current_password', 'password', 'password_confirmation'); + $this->reset('password', 'password_confirmation'); throw $e; } - auth()->user()->update([ + Auth::::user()->update([ 'password' => Hash::make($validated['password']), ]); diff --git a/stubs/breeze/livewire/resources/views/livewire/pages/auth/login.blade.php b/stubs/breeze/livewire/resources/views/livewire/pages/auth/login.blade.php index ef47890e..ff48aef9 100644 --- a/stubs/breeze/livewire/resources/views/livewire/pages/auth/login.blade.php +++ b/stubs/breeze/livewire/resources/views/livewire/pages/auth/login.blade.php @@ -1,71 +1,36 @@ validate(); - $this->ensureIsNotRateLimited(); - - if (! auth()->attempt($this->only(['email', 'password'], $this->remember))) { - RateLimiter::hit($this->throttleKey()); - - throw ValidationException::withMessages([ - 'email' => trans('auth.failed'), - ]); - } + $this->form->authenticate(); - RateLimiter::clear($this->throttleKey()); - - session()->regenerate(); + Session::regenerate(); $this->redirect( session('url.intended', RouteServiceProvider::HOME), navigate: true ); } - - protected function ensureIsNotRateLimited(): void - { - if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) { - return; - } - - event(new Lockout(request())); - - $seconds = RateLimiter::availableIn($this->throttleKey()); - - throw ValidationException::withMessages([ - 'email' => trans('auth.throttle', [ - 'seconds' => $seconds, - 'minutes' => ceil($seconds / 60), - ]), - ]); - } - - protected function throttleKey(): string - { - return Str::transliterate(Str::lower($this->email).'|'.request()->ip()); - } }; ?>
@@ -76,7 +41,7 @@ protected function throttleKey(): string
- +
@@ -84,7 +49,7 @@ protected function throttleKey(): string
- @@ -95,7 +60,7 @@ protected function throttleKey(): string
diff --git a/stubs/breeze/livewire/resources/views/livewire/pages/auth/register.blade.php b/stubs/breeze/livewire/resources/views/livewire/pages/auth/register.blade.php index 37668026..9259db07 100644 --- a/stubs/breeze/livewire/resources/views/livewire/pages/auth/register.blade.php +++ b/stubs/breeze/livewire/resources/views/livewire/pages/auth/register.blade.php @@ -3,6 +3,7 @@ use App\Models\User; use App\Providers\RouteServiceProvider; use Illuminate\Auth\Events\Registered; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rules; use Livewire\Attributes\Layout; @@ -18,6 +19,9 @@ public string $password_confirmation = ''; + /** + * Handle an incoming registration request. + */ public function register(): void { $validated = $this->validate([ @@ -30,7 +34,7 @@ public function register(): void event(new Registered($user = User::create($validated))); - auth()->login($user); + Auth::login($user); $this->redirect(RouteServiceProvider::HOME, navigate: true); } diff --git a/stubs/breeze/livewire/resources/views/livewire/profile/connected-accounts-form.blade.php b/stubs/breeze/livewire/resources/views/livewire/profile/connected-accounts-form.blade.php index 42c357fe..468033f6 100644 --- a/stubs/breeze/livewire/resources/views/livewire/profile/connected-accounts-form.blade.php +++ b/stubs/breeze/livewire/resources/views/livewire/profile/connected-accounts-form.blade.php @@ -1,18 +1,23 @@ validate(); + $this->validate([ + 'password' => ['required', 'string', 'current_password'] + ]); - auth()->user()->connectedAccounts() + Auth::user()->connectedAccounts() ->where('id', $id) ->delete(); @@ -37,14 +42,14 @@ public function removeAccount(string|int $id): void @foreach (JoelButcher\Socialstream\Socialstream::providers() as $provider) @php $account = null; - $account = auth()->user()->connectedAccounts->where('provider', $provider['id'])->first(); + $account = Auth::user()->connectedAccounts->where('provider', $provider['id'])->first(); @endphp @if (! is_null($account))
- @if ((auth()->user()->connectedAccounts->count() > 1 || ! is_null(auth()->user()->getAuthPassword()))) + @if ((Auth::user()->connectedAccounts->count() > 1 || ! is_null(Auth::user()->getAuthPassword()))) validate(); + $this->validate([ + 'password' => ['required', 'string', 'current_password'], + ]); - $user = tap(auth()->user(), fn () => auth()->logout()); + $user = tap(Auth::user(), $logout(...)); $user->connectedAccounts->each->delete(); $user->delete(); - session()->invalidate(); - session()->regenerateToken(); - $this->redirect('/', navigate: true); } }; ?> diff --git a/stubs/breeze/livewire/resources/views/livewire/profile/set-password-form.blade.php b/stubs/breeze/livewire/resources/views/livewire/profile/set-password-form.blade.php index 6b92556b..782ab0a5 100644 --- a/stubs/breeze/livewire/resources/views/livewire/profile/set-password-form.blade.php +++ b/stubs/breeze/livewire/resources/views/livewire/profile/set-password-form.blade.php @@ -1,5 +1,6 @@ ['required', 'string', Password::defaults(), 'confirmed'], ]); } catch (ValidationException $e) { - $this->reset('current_password', 'password', 'password_confirmation'); + $this->reset('password', 'password_confirmation'); throw $e; } - auth()->user()->update([ + Auth::user()->update([ 'password' => Hash::make($validated['password']), ]);