Skip to content

Commit

Permalink
[5.x] Align breeze livewire stacks (#311)
Browse files Browse the repository at this point in the history
* bump min breeze version

* refactor livewire class stack

* refactor livewire functional stack
  • Loading branch information
joelbutcher authored Nov 1, 2023
1 parent db295e4 commit baa28e0
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 120 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/Installer/Drivers/Breeze/BreezeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
<?php
use App\Livewire\Forms\LoginForm;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Session;
use function Livewire\Volt\form;
use function Livewire\Volt\layout;
use function Livewire\Volt\rules;
use function Livewire\Volt\state;
layout('layouts.guest');
state(['email' => '', '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),
Expand All @@ -66,15 +34,15 @@
<!-- Email Address -->
<div>
<x-input-label for="email" :value="__('Email')" />
<x-text-input wire:model="email" id="email" class="block mt-1 w-full" type="email" name="email" required autofocus autocomplete="username" />
<x-text-input wire:model="form.email" id="email" class="block mt-1 w-full" type="email" name="email" required autofocus autocomplete="username" />
<x-input-error :messages="$errors->get('email')" class="mt-2" />
</div>

<!-- Password -->
<div class="mt-4">
<x-input-label for="password" :value="__('Password')" />

<x-text-input wire:model="password" id="password" class="block mt-1 w-full"
<x-text-input wire:model="form.password" id="password" class="block mt-1 w-full"
type="password"
name="password"
required autocomplete="current-password" />
Expand All @@ -85,7 +53,7 @@
<!-- Remember Me -->
<div class="block mt-4">
<label for="remember" class="inline-flex items-center">
<input wire:model="remember" id="remember" type="checkbox" class="rounded dark:bg-gray-900 border-gray-300 dark:border-gray-700 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:focus:ring-indigo-600 dark:focus:ring-offset-gray-800" name="remember">
<input wire:model="form.remember" id="remember" type="checkbox" class="rounded dark:bg-gray-900 border-gray-300 dark:border-gray-700 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:focus:ring-indigo-600 dark:focus:ring-offset-gray-800" name="remember">
<span class="ml-2 text-sm text-gray-600 dark:text-gray-400">{{ __('Remember me') }}</span>
</label>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()],
]);
Expand All @@ -32,7 +33,7 @@
event(new Registered($user = User::create($validated)));
auth()->login($user);
Auth::login($user);
$this->redirect(RouteServiceProvider::HOME, navigate: true);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
use Illuminate\Support\Facades\Auth;
use function Livewire\Volt\rules;
use function Livewire\Volt\state;
Expand All @@ -10,7 +12,7 @@
$removeAccount = function (string|int $id) {
$this->validate();
auth()->user()->connectedAccounts()
Auth::user()->connectedAccounts()
->where('id', $id)
->delete();
Expand All @@ -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

<x-connected-account :provider="$provider" created-at="{{ $account?->created_at->diffForHumans() ?? null }}">
<x-slot name="action">
@if (! is_null($account))
<div class="flex items-center space-x-6">
@if ((auth()->user()->connectedAccounts->count() > 1 || ! is_null(auth()->user()->getAuthPassword())))
@if ((Auth::user()->connectedAccounts->count() > 1 || ! is_null(Auth::user()->getAuthPassword())))
<x-danger-button
x-data=""
x-on:click.prevent="$dispatch('open-modal', 'confirm-connected-account-deletion')"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?php
use App\Livewire\Actions\Logout;
use Illuminate\Support\Facades\Auth;
use function Livewire\Volt\rules;
use function Livewire\Volt\state;
state(['password' => '']);
rules(['password' => ['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);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\ValidationException;
Expand All @@ -16,12 +17,12 @@
try {
$validated = $this->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']),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,71 +1,36 @@
<?php
use App\Livewire\Forms\LoginForm;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Rule;
use Livewire\Volt\Component;
new #[Layout('layouts.guest')] class extends Component
{
#[Rule(['required', 'string', 'email'])]
public string $email = '';
#[Rule(['required', 'string'])]
public string $password = '';
#[Rule(['boolean'])]
public bool $remember = false;
public LoginForm $form;
/**
* Handle an incoming authentication request.
*/
public function login(): void
{
$this->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());
}
}; ?>

<div>
Expand All @@ -76,15 +41,15 @@ protected function throttleKey(): string
<!-- Email Address -->
<div>
<x-input-label for="email" :value="__('Email')" />
<x-text-input wire:model="email" id="email" class="block mt-1 w-full" type="email" name="email" required autofocus autocomplete="username" />
<x-text-input wire:model="form.email" id="email" class="block mt-1 w-full" type="email" name="email" required autofocus autocomplete="username" />
<x-input-error :messages="$errors->get('email')" class="mt-2" />
</div>

<!-- Password -->
<div class="mt-4">
<x-input-label for="password" :value="__('Password')" />

<x-text-input wire:model="password" id="password" class="block mt-1 w-full"
<x-text-input wire:model="form.password" id="password" class="block mt-1 w-full"
type="password"
name="password"
required autocomplete="current-password" />
Expand All @@ -95,7 +60,7 @@ protected function throttleKey(): string
<!-- Remember Me -->
<div class="block mt-4">
<label for="remember" class="inline-flex items-center">
<input wire:model="remember" id="remember" type="checkbox" class="rounded dark:bg-gray-900 border-gray-300 dark:border-gray-700 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:focus:ring-indigo-600 dark:focus:ring-offset-gray-800" name="remember">
<input wire:model="form.remember" id="remember" type="checkbox" class="rounded dark:bg-gray-900 border-gray-300 dark:border-gray-700 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:focus:ring-indigo-600 dark:focus:ring-offset-gray-800" name="remember">
<span class="ml-2 text-sm text-gray-600 dark:text-gray-400">{{ __('Remember me') }}</span>
</label>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,9 @@
public string $password_confirmation = '';
/**
* Handle an incoming registration request.
*/
public function register(): void
{
$validated = $this->validate([
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php
use Livewire\Attributes\Rule;
use Illuminate\Support\Facades\Auth;
use Livewire\Volt\Component;
new class extends Component
{
#[Rule(['required', 'string', 'current_password'])]
public string $password = '';
/**
* Delete the given connected account for the currently authenticated user.
*/
public function removeAccount(string|int $id): void
{
$this->validate();
$this->validate([
'password' => ['required', 'string', 'current_password']
]);
auth()->user()->connectedAccounts()
Auth::user()->connectedAccounts()
->where('id', $id)
->delete();
Expand All @@ -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

<x-connected-account :provider="$provider" created-at="{{ $account?->created_at->diffForHumans() ?? null }}">
<x-slot name="action">
@if (! is_null($account))
<div class="flex items-center space-x-6">
@if ((auth()->user()->connectedAccounts->count() > 1 || ! is_null(auth()->user()->getAuthPassword())))
@if ((Auth::user()->connectedAccounts->count() > 1 || ! is_null(Auth::user()->getAuthPassword())))
<x-danger-button
x-data=""
x-on:click.prevent="$dispatch('open-modal', 'confirm-connected-account-deletion')"
Expand Down
Loading

0 comments on commit baa28e0

Please sign in to comment.