From 72033baaaf71c9344562c2f97f0c01b21b3a0e73 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 27 Jun 2024 10:18:20 +0800 Subject: [PATCH 1/3] Allow `PasswordBroker` and `redirect()->intended()` responses to be resolved via the Container Signed-off-by: Mior Muhammad Zaki --- src/FortifyServiceProvider.php | 6 ++++ ...mailVerificationNotificationController.php | 3 +- .../EmailVerificationPromptController.php | 3 +- .../Controllers/NewPasswordController.php | 2 +- .../PasswordResetLinkController.php | 2 +- src/Http/Responses/RedirectAsIntended.php | 31 +++++++++++++++++++ 6 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/Http/Responses/RedirectAsIntended.php diff --git a/src/FortifyServiceProvider.php b/src/FortifyServiceProvider.php index 4dd7c22d..a17ec211 100644 --- a/src/FortifyServiceProvider.php +++ b/src/FortifyServiceProvider.php @@ -2,9 +2,11 @@ namespace Laravel\Fortify; +use Illuminate\Contracts\Auth\PasswordBroker; use Illuminate\Contracts\Auth\StatefulGuard; use Illuminate\Contracts\Cache\Repository; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Password; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Laravel\Fortify\Contracts\EmailVerificationNotificationSentResponse as EmailVerificationNotificationSentResponseContract; @@ -73,6 +75,10 @@ public function register() $this->app->bind(StatefulGuard::class, function () { return Auth::guard(config('fortify.guard', null)); }); + + $this->app->bind(PasswordBroker::class, function () { + return Password::broker(config('fortify.passwords')); + }); } /** diff --git a/src/Http/Controllers/EmailVerificationNotificationController.php b/src/Http/Controllers/EmailVerificationNotificationController.php index 3a270920..654fc7c8 100644 --- a/src/Http/Controllers/EmailVerificationNotificationController.php +++ b/src/Http/Controllers/EmailVerificationNotificationController.php @@ -7,6 +7,7 @@ use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\EmailVerificationNotificationSentResponse; use Laravel\Fortify\Fortify; +use Laravel\Fortify\Http\Responses\RedirectAsIntended; class EmailVerificationNotificationController extends Controller { @@ -21,7 +22,7 @@ public function store(Request $request) if ($request->user()->hasVerifiedEmail()) { return $request->wantsJson() ? new JsonResponse('', 204) - : redirect()->intended(Fortify::redirects('email-verification')); + : app(RedirectAsIntended::class, ['name' => 'email-verification']); } $request->user()->sendEmailVerificationNotification(); diff --git a/src/Http/Controllers/EmailVerificationPromptController.php b/src/Http/Controllers/EmailVerificationPromptController.php index 40f0e761..db17c77c 100644 --- a/src/Http/Controllers/EmailVerificationPromptController.php +++ b/src/Http/Controllers/EmailVerificationPromptController.php @@ -6,6 +6,7 @@ use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\VerifyEmailViewResponse; use Laravel\Fortify\Fortify; +use Laravel\Fortify\Http\Responses\RedirectAsIntended; class EmailVerificationPromptController extends Controller { @@ -18,7 +19,7 @@ class EmailVerificationPromptController extends Controller public function __invoke(Request $request) { return $request->user()->hasVerifiedEmail() - ? redirect()->intended(Fortify::redirects('email-verification')) + ? app(RedirectAsIntended::class, ['name' => 'email-verification']) : app(VerifyEmailViewResponse::class); } } diff --git a/src/Http/Controllers/NewPasswordController.php b/src/Http/Controllers/NewPasswordController.php index d8e3d4b2..8d825527 100644 --- a/src/Http/Controllers/NewPasswordController.php +++ b/src/Http/Controllers/NewPasswordController.php @@ -87,6 +87,6 @@ function ($user) use ($request) { */ protected function broker(): PasswordBroker { - return Password::broker(config('fortify.passwords')); + return app(PasswordBroker::class); } } diff --git a/src/Http/Controllers/PasswordResetLinkController.php b/src/Http/Controllers/PasswordResetLinkController.php index 16834beb..52990e91 100644 --- a/src/Http/Controllers/PasswordResetLinkController.php +++ b/src/Http/Controllers/PasswordResetLinkController.php @@ -54,6 +54,6 @@ public function store(Request $request): Responsable */ protected function broker(): PasswordBroker { - return Password::broker(config('fortify.passwords')); + return app(PasswordBroker::class); } } diff --git a/src/Http/Responses/RedirectAsIntended.php b/src/Http/Responses/RedirectAsIntended.php new file mode 100644 index 00000000..8d79642a --- /dev/null +++ b/src/Http/Responses/RedirectAsIntended.php @@ -0,0 +1,31 @@ +intended(Fortify::redirects($this->name)); + } +} From 87f7d7522df7b040d05d7a4a377ae55eb75783ea Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 27 Jun 2024 02:19:41 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- src/Http/Controllers/EmailVerificationNotificationController.php | 1 - src/Http/Controllers/EmailVerificationPromptController.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Http/Controllers/EmailVerificationNotificationController.php b/src/Http/Controllers/EmailVerificationNotificationController.php index 654fc7c8..05196b30 100644 --- a/src/Http/Controllers/EmailVerificationNotificationController.php +++ b/src/Http/Controllers/EmailVerificationNotificationController.php @@ -6,7 +6,6 @@ use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\EmailVerificationNotificationSentResponse; -use Laravel\Fortify\Fortify; use Laravel\Fortify\Http\Responses\RedirectAsIntended; class EmailVerificationNotificationController extends Controller diff --git a/src/Http/Controllers/EmailVerificationPromptController.php b/src/Http/Controllers/EmailVerificationPromptController.php index db17c77c..c09a9b30 100644 --- a/src/Http/Controllers/EmailVerificationPromptController.php +++ b/src/Http/Controllers/EmailVerificationPromptController.php @@ -5,7 +5,6 @@ use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\VerifyEmailViewResponse; -use Laravel\Fortify\Fortify; use Laravel\Fortify\Http\Responses\RedirectAsIntended; class EmailVerificationPromptController extends Controller From e16e54a92315c21ac5ba7246d9a18d7a8966dffa Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 4 Jul 2024 07:15:41 +0800 Subject: [PATCH 3/3] Apply suggestions from code review --- src/FortifyServiceProvider.php | 6 ------ src/Http/Controllers/NewPasswordController.php | 2 +- src/Http/Controllers/PasswordResetLinkController.php | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/FortifyServiceProvider.php b/src/FortifyServiceProvider.php index a17ec211..4dd7c22d 100644 --- a/src/FortifyServiceProvider.php +++ b/src/FortifyServiceProvider.php @@ -2,11 +2,9 @@ namespace Laravel\Fortify; -use Illuminate\Contracts\Auth\PasswordBroker; use Illuminate\Contracts\Auth\StatefulGuard; use Illuminate\Contracts\Cache\Repository; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Password; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Laravel\Fortify\Contracts\EmailVerificationNotificationSentResponse as EmailVerificationNotificationSentResponseContract; @@ -75,10 +73,6 @@ public function register() $this->app->bind(StatefulGuard::class, function () { return Auth::guard(config('fortify.guard', null)); }); - - $this->app->bind(PasswordBroker::class, function () { - return Password::broker(config('fortify.passwords')); - }); } /** diff --git a/src/Http/Controllers/NewPasswordController.php b/src/Http/Controllers/NewPasswordController.php index 8d825527..d8e3d4b2 100644 --- a/src/Http/Controllers/NewPasswordController.php +++ b/src/Http/Controllers/NewPasswordController.php @@ -87,6 +87,6 @@ function ($user) use ($request) { */ protected function broker(): PasswordBroker { - return app(PasswordBroker::class); + return Password::broker(config('fortify.passwords')); } } diff --git a/src/Http/Controllers/PasswordResetLinkController.php b/src/Http/Controllers/PasswordResetLinkController.php index 52990e91..16834beb 100644 --- a/src/Http/Controllers/PasswordResetLinkController.php +++ b/src/Http/Controllers/PasswordResetLinkController.php @@ -54,6 +54,6 @@ public function store(Request $request): Responsable */ protected function broker(): PasswordBroker { - return app(PasswordBroker::class); + return Password::broker(config('fortify.passwords')); } }