From ea01ccaed492824a6e4bad19254afc4b75c5865d Mon Sep 17 00:00:00 2001 From: "Stainlee (Mentor Laptop)" Date: Mon, 21 Sep 2020 22:59:21 +0530 Subject: [PATCH 1/2] Send JSON response if request is an AJAX request --- .../Controllers/EmailVerificationNotificationController.php | 5 +++-- src/Http/Controllers/PasswordController.php | 3 ++- src/Http/Controllers/ProfileInformationController.php | 3 ++- src/Http/Controllers/RecoveryCodeController.php | 3 ++- src/Http/Controllers/TwoFactorAuthenticationController.php | 5 +++-- src/Http/Responses/LogoutResponse.php | 3 ++- src/Http/Responses/PasswordConfirmedResponse.php | 3 ++- src/Http/Responses/RegisterResponse.php | 3 ++- src/Http/Responses/TwoFactorLoginResponse.php | 3 ++- 9 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Http/Controllers/EmailVerificationNotificationController.php b/src/Http/Controllers/EmailVerificationNotificationController.php index aa8d4ac8..a14e5278 100644 --- a/src/Http/Controllers/EmailVerificationNotificationController.php +++ b/src/Http/Controllers/EmailVerificationNotificationController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller; class EmailVerificationNotificationController extends Controller @@ -18,14 +19,14 @@ public function store(Request $request) { if ($request->user()->hasVerifiedEmail()) { return $request->wantsJson() - ? new Response('', 204) + ? new JsonResponse('', 204) : redirect(config('fortify.home')); } $request->user()->sendEmailVerificationNotification(); return $request->wantsJson() - ? new Response('', 202) + ? new JsonResponse('', 202) : back()->with('status', 'verification-link-sent'); } } diff --git a/src/Http/Controllers/PasswordController.php b/src/Http/Controllers/PasswordController.php index ea5adee2..df7e0f9b 100644 --- a/src/Http/Controllers/PasswordController.php +++ b/src/Http/Controllers/PasswordController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\UpdatesUserPasswords; @@ -21,7 +22,7 @@ public function update(Request $request, UpdatesUserPasswords $updater) $updater->update($request->user(), $request->all()); return $request->wantsJson() - ? new Response('', 200) + ? new JsonResponse('', 200) : back()->with('status', 'password-updated'); } } diff --git a/src/Http/Controllers/ProfileInformationController.php b/src/Http/Controllers/ProfileInformationController.php index be4c4631..8e5e9c0f 100644 --- a/src/Http/Controllers/ProfileInformationController.php +++ b/src/Http/Controllers/ProfileInformationController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\UpdatesUserProfileInformation; @@ -22,7 +23,7 @@ public function update(Request $request, $updater->update($request->user(), $request->all()); return $request->wantsJson() - ? new Response('', 200) + ? new JsonResponse('', 200) : back()->with('status', 'profile-information-updated'); } } diff --git a/src/Http/Controllers/RecoveryCodeController.php b/src/Http/Controllers/RecoveryCodeController.php index f6cca06b..5dae4350 100644 --- a/src/Http/Controllers/RecoveryCodeController.php +++ b/src/Http/Controllers/RecoveryCodeController.php @@ -3,6 +3,7 @@ namespace Laravel\Fortify\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller; use Laravel\Fortify\Actions\GenerateNewRecoveryCodes; @@ -38,7 +39,7 @@ public function store(Request $request, GenerateNewRecoveryCodes $generate) $generate($request->user()); return $request->wantsJson() - ? response('', 200) + ? new JsonResponse('', 200) : back()->with('status', 'recovery-codes-generated'); } } diff --git a/src/Http/Controllers/TwoFactorAuthenticationController.php b/src/Http/Controllers/TwoFactorAuthenticationController.php index a1f434e6..10cb6f67 100644 --- a/src/Http/Controllers/TwoFactorAuthenticationController.php +++ b/src/Http/Controllers/TwoFactorAuthenticationController.php @@ -3,6 +3,7 @@ namespace Laravel\Fortify\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller; use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; use Laravel\Fortify\Actions\EnableTwoFactorAuthentication; @@ -21,7 +22,7 @@ public function store(Request $request, EnableTwoFactorAuthentication $enable) $enable($request->user()); return $request->wantsJson() - ? response('', 200) + ? new JsonResponse('', 200) : back()->with('status', 'two-factor-authentication-enabled'); } @@ -37,7 +38,7 @@ public function destroy(Request $request, DisableTwoFactorAuthentication $disabl $disable($request->user()); return $request->wantsJson() - ? response('', 200) + ? new JsonResponse('', 200) : back()->with('status', 'two-factor-authentication-disabled'); } } diff --git a/src/Http/Responses/LogoutResponse.php b/src/Http/Responses/LogoutResponse.php index 23e59e5f..ab1b39c9 100644 --- a/src/Http/Responses/LogoutResponse.php +++ b/src/Http/Responses/LogoutResponse.php @@ -3,6 +3,7 @@ namespace Laravel\Fortify\Http\Responses; use Illuminate\Http\Response; +use Illuminate\Http\JsonResponse; use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract; class LogoutResponse implements LogoutResponseContract @@ -16,7 +17,7 @@ class LogoutResponse implements LogoutResponseContract public function toResponse($request) { return $request->wantsJson() - ? new Response('', 204) + ? new JsonResponse('', 204) : redirect('/'); } } diff --git a/src/Http/Responses/PasswordConfirmedResponse.php b/src/Http/Responses/PasswordConfirmedResponse.php index c51bec93..0b7436e2 100644 --- a/src/Http/Responses/PasswordConfirmedResponse.php +++ b/src/Http/Responses/PasswordConfirmedResponse.php @@ -3,6 +3,7 @@ namespace Laravel\Fortify\Http\Responses; use Illuminate\Http\Response; +use Illuminate\Http\JsonResponse; use Laravel\Fortify\Contracts\PasswordConfirmedResponse as PasswordConfirmedResponseContract; class PasswordConfirmedResponse implements PasswordConfirmedResponseContract @@ -16,7 +17,7 @@ class PasswordConfirmedResponse implements PasswordConfirmedResponseContract public function toResponse($request) { return $request->wantsJson() - ? new Response('', 201) + ? new JsonResponse('', 201) : redirect()->intended(config('fortify.home')); } } diff --git a/src/Http/Responses/RegisterResponse.php b/src/Http/Responses/RegisterResponse.php index e8920921..1c933613 100644 --- a/src/Http/Responses/RegisterResponse.php +++ b/src/Http/Responses/RegisterResponse.php @@ -3,6 +3,7 @@ namespace Laravel\Fortify\Http\Responses; use Illuminate\Http\Response; +use Illuminate\Http\JsonResponse; use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract; class RegisterResponse implements RegisterResponseContract @@ -16,7 +17,7 @@ class RegisterResponse implements RegisterResponseContract public function toResponse($request) { return $request->wantsJson() - ? new Response('', 201) + ? new JsonResponse('', 201) : redirect(config('fortify.home')); } } diff --git a/src/Http/Responses/TwoFactorLoginResponse.php b/src/Http/Responses/TwoFactorLoginResponse.php index 694c9e1a..816008aa 100644 --- a/src/Http/Responses/TwoFactorLoginResponse.php +++ b/src/Http/Responses/TwoFactorLoginResponse.php @@ -2,6 +2,7 @@ namespace Laravel\Fortify\Http\Responses; +use Illuminate\Http\JsonResponse; use Laravel\Fortify\Contracts\TwoFactorLoginResponse as TwoFactorLoginResponseContract; class TwoFactorLoginResponse implements TwoFactorLoginResponseContract @@ -15,7 +16,7 @@ class TwoFactorLoginResponse implements TwoFactorLoginResponseContract public function toResponse($request) { return $request->wantsJson() - ? response('', 204) + ? new JsonResponse('', 204) : redirect(config('fortify.home')); } } From 3cfe68440a3d0c78fcdcc3233f8a77f015d46e60 Mon Sep 17 00:00:00 2001 From: "Stainlee (Mentor Laptop)" Date: Mon, 21 Sep 2020 23:21:32 +0530 Subject: [PATCH 2/2] Send JSON response if request is an AJAX request --- .../Controllers/EmailVerificationNotificationController.php | 3 +-- src/Http/Controllers/PasswordController.php | 3 +-- src/Http/Controllers/ProfileInformationController.php | 3 +-- src/Http/Controllers/RecoveryCodeController.php | 2 +- src/Http/Controllers/TwoFactorAuthenticationController.php | 2 +- src/Http/Responses/LogoutResponse.php | 2 +- src/Http/Responses/PasswordConfirmedResponse.php | 2 +- src/Http/Responses/RegisterResponse.php | 2 +- 8 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/Http/Controllers/EmailVerificationNotificationController.php b/src/Http/Controllers/EmailVerificationNotificationController.php index a14e5278..ec9e9034 100644 --- a/src/Http/Controllers/EmailVerificationNotificationController.php +++ b/src/Http/Controllers/EmailVerificationNotificationController.php @@ -2,9 +2,8 @@ namespace Laravel\Fortify\Http\Controllers; -use Illuminate\Http\Request; -use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Illuminate\Routing\Controller; class EmailVerificationNotificationController extends Controller diff --git a/src/Http/Controllers/PasswordController.php b/src/Http/Controllers/PasswordController.php index df7e0f9b..e8ffb12b 100644 --- a/src/Http/Controllers/PasswordController.php +++ b/src/Http/Controllers/PasswordController.php @@ -2,9 +2,8 @@ namespace Laravel\Fortify\Http\Controllers; -use Illuminate\Http\Request; -use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\UpdatesUserPasswords; diff --git a/src/Http/Controllers/ProfileInformationController.php b/src/Http/Controllers/ProfileInformationController.php index 8e5e9c0f..90795b71 100644 --- a/src/Http/Controllers/ProfileInformationController.php +++ b/src/Http/Controllers/ProfileInformationController.php @@ -2,9 +2,8 @@ namespace Laravel\Fortify\Http\Controllers; -use Illuminate\Http\Request; -use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Laravel\Fortify\Contracts\UpdatesUserProfileInformation; diff --git a/src/Http/Controllers/RecoveryCodeController.php b/src/Http/Controllers/RecoveryCodeController.php index 5dae4350..8fda352a 100644 --- a/src/Http/Controllers/RecoveryCodeController.php +++ b/src/Http/Controllers/RecoveryCodeController.php @@ -2,8 +2,8 @@ namespace Laravel\Fortify\Http\Controllers; -use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Laravel\Fortify\Actions\GenerateNewRecoveryCodes; diff --git a/src/Http/Controllers/TwoFactorAuthenticationController.php b/src/Http/Controllers/TwoFactorAuthenticationController.php index 10cb6f67..23809083 100644 --- a/src/Http/Controllers/TwoFactorAuthenticationController.php +++ b/src/Http/Controllers/TwoFactorAuthenticationController.php @@ -2,8 +2,8 @@ namespace Laravel\Fortify\Http\Controllers; -use Illuminate\Http\Request; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; use Laravel\Fortify\Actions\EnableTwoFactorAuthentication; diff --git a/src/Http/Responses/LogoutResponse.php b/src/Http/Responses/LogoutResponse.php index ab1b39c9..abe7128a 100644 --- a/src/Http/Responses/LogoutResponse.php +++ b/src/Http/Responses/LogoutResponse.php @@ -2,8 +2,8 @@ namespace Laravel\Fortify\Http\Responses; -use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Response; use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract; class LogoutResponse implements LogoutResponseContract diff --git a/src/Http/Responses/PasswordConfirmedResponse.php b/src/Http/Responses/PasswordConfirmedResponse.php index 0b7436e2..6dc7653b 100644 --- a/src/Http/Responses/PasswordConfirmedResponse.php +++ b/src/Http/Responses/PasswordConfirmedResponse.php @@ -2,8 +2,8 @@ namespace Laravel\Fortify\Http\Responses; -use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Response; use Laravel\Fortify\Contracts\PasswordConfirmedResponse as PasswordConfirmedResponseContract; class PasswordConfirmedResponse implements PasswordConfirmedResponseContract diff --git a/src/Http/Responses/RegisterResponse.php b/src/Http/Responses/RegisterResponse.php index 1c933613..3091d95e 100644 --- a/src/Http/Responses/RegisterResponse.php +++ b/src/Http/Responses/RegisterResponse.php @@ -2,8 +2,8 @@ namespace Laravel\Fortify\Http\Responses; -use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Response; use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract; class RegisterResponse implements RegisterResponseContract