From 4d21766ce034623aec966d7768b9739967095cc7 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Fri, 23 Jul 2021 15:19:22 -0300 Subject: [PATCH 1/4] feat: redirection customization --- config/fortify.php | 7 +++++++ src/Fortify.php | 11 +++++++++++ .../EmailVerificationNotificationController.php | 3 ++- .../Controllers/EmailVerificationPromptController.php | 3 ++- src/Http/Controllers/VerifyEmailController.php | 5 +++-- .../Responses/FailedPasswordConfirmationResponse.php | 3 +-- src/Http/Responses/FailedPasswordResetResponse.php | 6 +++--- src/Http/Responses/LoginResponse.php | 3 ++- src/Http/Responses/LogoutResponse.php | 3 ++- src/Http/Responses/PasswordConfirmedResponse.php | 4 ++-- src/Http/Responses/RegisterResponse.php | 4 ++-- src/Http/Responses/TwoFactorLoginResponse.php | 3 ++- 12 files changed, 39 insertions(+), 16 deletions(-) diff --git a/config/fortify.php b/config/fortify.php index 007d82d1..b76b6a36 100644 --- a/config/fortify.php +++ b/config/fortify.php @@ -15,6 +15,13 @@ 'limiters' => [ 'login' => null, ], + 'redirects' => [ + 'login' => null, + 'logout' => null, + 'password-confirmed' => null, + 'register' => null, + 'email-verification' => null, + ], 'features' => [ Features::registration(), Features::resetPasswords(), diff --git a/src/Fortify.php b/src/Fortify.php index 0e4d4406..b5ec82d3 100644 --- a/src/Fortify.php +++ b/src/Fortify.php @@ -65,6 +65,17 @@ public static function email() return config('fortify.email', 'email'); } + /** + * Get the redirect path. + * + * @param string $redirect + * @return string + */ + public static function redirects(string $redirect, $default = null) + { + return config('fortify.redirects.'.$redirect, $default) ?? config('fortify.home'); + } + /** * Register the views for Fortify using conventional names under the given namespace. * diff --git a/src/Http/Controllers/EmailVerificationNotificationController.php b/src/Http/Controllers/EmailVerificationNotificationController.php index 75055653..e3dafac8 100644 --- a/src/Http/Controllers/EmailVerificationNotificationController.php +++ b/src/Http/Controllers/EmailVerificationNotificationController.php @@ -5,6 +5,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Routing\Controller; +use Laravel\Fortify\Fortify; class EmailVerificationNotificationController extends Controller { @@ -19,7 +20,7 @@ public function store(Request $request) if ($request->user()->hasVerifiedEmail()) { return $request->wantsJson() ? new JsonResponse('', 204) - : redirect()->intended(config('fortify.home')); + : redirect()->intended(Fortify::redirects('email-verification')); } $request->user()->sendEmailVerificationNotification(); diff --git a/src/Http/Controllers/EmailVerificationPromptController.php b/src/Http/Controllers/EmailVerificationPromptController.php index 33b9ad57..40f0e761 100644 --- a/src/Http/Controllers/EmailVerificationPromptController.php +++ b/src/Http/Controllers/EmailVerificationPromptController.php @@ -5,6 +5,7 @@ use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\VerifyEmailViewResponse; +use Laravel\Fortify\Fortify; class EmailVerificationPromptController extends Controller { @@ -17,7 +18,7 @@ class EmailVerificationPromptController extends Controller public function __invoke(Request $request) { return $request->user()->hasVerifiedEmail() - ? redirect()->intended(config('fortify.home')) + ? redirect()->intended(Fortify::redirects('email-verification')) : app(VerifyEmailViewResponse::class); } } diff --git a/src/Http/Controllers/VerifyEmailController.php b/src/Http/Controllers/VerifyEmailController.php index 88ff348c..a94be9dc 100644 --- a/src/Http/Controllers/VerifyEmailController.php +++ b/src/Http/Controllers/VerifyEmailController.php @@ -5,6 +5,7 @@ use Illuminate\Auth\Events\Verified; use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller; +use Laravel\Fortify\Fortify; use Laravel\Fortify\Http\Requests\VerifyEmailRequest; class VerifyEmailController extends Controller @@ -20,7 +21,7 @@ public function __invoke(VerifyEmailRequest $request) if ($request->user()->hasVerifiedEmail()) { return $request->wantsJson() ? new JsonResponse('', 204) - : redirect()->intended(config('fortify.home').'?verified=1'); + : redirect()->intended(Fortify::redirects('email-verification').'?verified=1'); } if ($request->user()->markEmailAsVerified()) { @@ -29,6 +30,6 @@ public function __invoke(VerifyEmailRequest $request) return $request->wantsJson() ? new JsonResponse('', 202) - : redirect()->intended(config('fortify.home').'?verified=1'); + : redirect()->intended(Fortify::redirects('email-verification').'?verified=1'); } } diff --git a/src/Http/Responses/FailedPasswordConfirmationResponse.php b/src/Http/Responses/FailedPasswordConfirmationResponse.php index 3ac4980b..8456b718 100644 --- a/src/Http/Responses/FailedPasswordConfirmationResponse.php +++ b/src/Http/Responses/FailedPasswordConfirmationResponse.php @@ -2,7 +2,6 @@ namespace Laravel\Fortify\Http\Responses; -use Illuminate\Http\Response; use Illuminate\Validation\ValidationException; use Laravel\Fortify\Contracts\FailedPasswordConfirmationResponse as FailedPasswordConfirmationResponseContract; @@ -24,6 +23,6 @@ public function toResponse($request) ]); } - return redirect()->back()->withErrors(['password' => $message]); + return back()->withErrors(['password' => $message]); } } diff --git a/src/Http/Responses/FailedPasswordResetResponse.php b/src/Http/Responses/FailedPasswordResetResponse.php index 970f6755..a6be6627 100644 --- a/src/Http/Responses/FailedPasswordResetResponse.php +++ b/src/Http/Responses/FailedPasswordResetResponse.php @@ -39,8 +39,8 @@ public function toResponse($request) ]); } - return redirect()->back() - ->withInput($request->only('email')) - ->withErrors(['email' => trans($this->status)]); + return back() + ->withInput($request->only('email')) + ->withErrors(['email' => trans($this->status)]); } } diff --git a/src/Http/Responses/LoginResponse.php b/src/Http/Responses/LoginResponse.php index ec3fe973..0a214f71 100644 --- a/src/Http/Responses/LoginResponse.php +++ b/src/Http/Responses/LoginResponse.php @@ -3,6 +3,7 @@ namespace Laravel\Fortify\Http\Responses; use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract; +use Laravel\Fortify\Fortify; class LoginResponse implements LoginResponseContract { @@ -16,6 +17,6 @@ public function toResponse($request) { return $request->wantsJson() ? response()->json(['two_factor' => false]) - : redirect()->intended(config('fortify.home')); + : redirect()->intended(Fortify::redirects('login')); } } diff --git a/src/Http/Responses/LogoutResponse.php b/src/Http/Responses/LogoutResponse.php index c525990e..f6596b4e 100644 --- a/src/Http/Responses/LogoutResponse.php +++ b/src/Http/Responses/LogoutResponse.php @@ -4,6 +4,7 @@ use Illuminate\Http\JsonResponse; use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract; +use Laravel\Fortify\Fortify; class LogoutResponse implements LogoutResponseContract { @@ -17,6 +18,6 @@ public function toResponse($request) { return $request->wantsJson() ? new JsonResponse('', 204) - : redirect('/'); + : redirect(Fortify::redirects('logout', '/')); } } diff --git a/src/Http/Responses/PasswordConfirmedResponse.php b/src/Http/Responses/PasswordConfirmedResponse.php index 6dc7653b..f247a74a 100644 --- a/src/Http/Responses/PasswordConfirmedResponse.php +++ b/src/Http/Responses/PasswordConfirmedResponse.php @@ -3,8 +3,8 @@ namespace Laravel\Fortify\Http\Responses; use Illuminate\Http\JsonResponse; -use Illuminate\Http\Response; use Laravel\Fortify\Contracts\PasswordConfirmedResponse as PasswordConfirmedResponseContract; +use Laravel\Fortify\Fortify; class PasswordConfirmedResponse implements PasswordConfirmedResponseContract { @@ -18,6 +18,6 @@ public function toResponse($request) { return $request->wantsJson() ? new JsonResponse('', 201) - : redirect()->intended(config('fortify.home')); + : redirect()->intended(Fortify::redirects('password-confirmed')); } } diff --git a/src/Http/Responses/RegisterResponse.php b/src/Http/Responses/RegisterResponse.php index 2170f45c..2e42cd31 100644 --- a/src/Http/Responses/RegisterResponse.php +++ b/src/Http/Responses/RegisterResponse.php @@ -3,8 +3,8 @@ namespace Laravel\Fortify\Http\Responses; use Illuminate\Http\JsonResponse; -use Illuminate\Http\Response; use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract; +use Laravel\Fortify\Fortify; class RegisterResponse implements RegisterResponseContract { @@ -18,6 +18,6 @@ public function toResponse($request) { return $request->wantsJson() ? new JsonResponse('', 201) - : redirect()->intended(config('fortify.home')); + : redirect()->intended(Fortify::redirects('register')); } } diff --git a/src/Http/Responses/TwoFactorLoginResponse.php b/src/Http/Responses/TwoFactorLoginResponse.php index 2c0c5892..07c9753c 100644 --- a/src/Http/Responses/TwoFactorLoginResponse.php +++ b/src/Http/Responses/TwoFactorLoginResponse.php @@ -4,6 +4,7 @@ use Illuminate\Http\JsonResponse; use Laravel\Fortify\Contracts\TwoFactorLoginResponse as TwoFactorLoginResponseContract; +use Laravel\Fortify\Fortify; class TwoFactorLoginResponse implements TwoFactorLoginResponseContract { @@ -17,6 +18,6 @@ public function toResponse($request) { return $request->wantsJson() ? new JsonResponse('', 204) - : redirect()->intended(config('fortify.home')); + : redirect()->intended(Fortify::redirects('login')); } } From 6d19969b348d40356802dcd649e8160347476759 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Fri, 23 Jul 2021 15:27:29 -0300 Subject: [PATCH 2/4] fix redirects default --- src/Fortify.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fortify.php b/src/Fortify.php index b5ec82d3..15e04415 100644 --- a/src/Fortify.php +++ b/src/Fortify.php @@ -73,7 +73,7 @@ public static function email() */ public static function redirects(string $redirect, $default = null) { - return config('fortify.redirects.'.$redirect, $default) ?? config('fortify.home'); + return config('fortify.redirects.'.$redirect) ?? $default ?? config('fortify.home'); } /** From a69dee8a3659525844bc274207952c36e0517501 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 26 Jul 2021 08:23:33 -0500 Subject: [PATCH 3/4] formatting --- config/fortify.php | 2 +- src/Fortify.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/fortify.php b/config/fortify.php index b76b6a36..56765c7d 100644 --- a/config/fortify.php +++ b/config/fortify.php @@ -18,7 +18,7 @@ 'redirects' => [ 'login' => null, 'logout' => null, - 'password-confirmed' => null, + 'password-confirmation' => null, 'register' => null, 'email-verification' => null, ], diff --git a/src/Fortify.php b/src/Fortify.php index 15e04415..60ae8b38 100644 --- a/src/Fortify.php +++ b/src/Fortify.php @@ -66,7 +66,7 @@ public static function email() } /** - * Get the redirect path. + * Get a completion redirect path for a specific feature. * * @param string $redirect * @return string From 5204a91b55427be2cc704457918f1823c1f92a25 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 26 Jul 2021 08:24:09 -0500 Subject: [PATCH 4/4] fix --- src/Http/Responses/PasswordConfirmedResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Responses/PasswordConfirmedResponse.php b/src/Http/Responses/PasswordConfirmedResponse.php index f247a74a..33d7e78e 100644 --- a/src/Http/Responses/PasswordConfirmedResponse.php +++ b/src/Http/Responses/PasswordConfirmedResponse.php @@ -18,6 +18,6 @@ public function toResponse($request) { return $request->wantsJson() ? new JsonResponse('', 201) - : redirect()->intended(Fortify::redirects('password-confirmed')); + : redirect()->intended(Fortify::redirects('password-confirmation')); } }