From c060b008b122662eee8df1b0b1a6289be52fc0e8 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Fri, 21 May 2021 23:37:44 +0200 Subject: [PATCH] feat: handle errors in production (#801) --- app/Exceptions/Handler.php | 25 +++--- app/Http/Controllers/Auth/LoginController.php | 20 ++--- app/Http/Kernel.php | 2 +- app/Http/Middleware/HandleInertiaRequests.php | 73 ++++++++++++++++ app/Providers/AppServiceProvider.php | 52 ------------ resources/js/Pages/Auth/Login.vue | 37 +++++---- resources/js/Pages/Error.vue | 83 +++++++++++++++++++ resources/js/Shared/Errors.vue | 5 +- resources/lang/en/errors.php | 21 +++++ 9 files changed, 223 insertions(+), 95 deletions(-) create mode 100644 app/Http/Middleware/HandleInertiaRequests.php create mode 100644 resources/js/Pages/Error.vue create mode 100644 resources/lang/en/errors.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 70d8dc71b..a632ff2fb 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,12 +3,7 @@ namespace App\Exceptions; use Throwable; -use Illuminate\Support\Facades\Redirect; -use Illuminate\Session\TokenMismatchException; -use Illuminate\Validation\ValidationException; -use Illuminate\Auth\Access\AuthorizationException; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Symfony\Component\HttpKernel\Exception\HttpException; +use Inertia\Inertia; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler @@ -19,10 +14,6 @@ class Handler extends ExceptionHandler * @var array */ protected $dontReport = [ - AuthorizationException::class, - HttpException::class, - ModelNotFoundException::class, - ValidationException::class, ]; /** @@ -48,11 +39,19 @@ class Handler extends ExceptionHandler */ public function render($request, Throwable $e) { - if ($e instanceof TokenMismatchException) { - return Redirect::route('login'); + $response = parent::render($request, $e); + + if (! app()->environment('local') && in_array($response->getStatusCode(), [500, 503, 401, 404, 403])) { + return Inertia::render('Error', ['status' => $response->getStatusCode()]) + ->toResponse($request) + ->setStatusCode($response->getStatusCode()); + } elseif ($response->getStatusCode() === 419) { + return back()->with([ + 'message' => trans('errors.message_419'), + ]); } - return parent::render($request, $e); + return $response; } /** diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 6bdc2b1a1..f2188768b 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -4,8 +4,9 @@ use Inertia\Inertia; use Illuminate\Http\Request; -use Illuminate\Http\JsonResponse; use App\Http\Controllers\Controller; +use Illuminate\Support\Facades\Redirect; +use Illuminate\Support\Facades\Response; use Illuminate\Foundation\Auth\AuthenticatesUsers; class LoginController extends Controller @@ -37,16 +38,15 @@ public function showLoginForm(): \Inertia\Response * * @param \Illuminate\Http\Request $request * @param mixed $user - * @return mixed + * @return \Symfony\Component\HttpFoundation\Response */ protected function authenticated(Request $request, $user) { $path = $request->session()->pull('url.intended', route('home')); - return new JsonResponse([ - 'success' => true, - 'redirect' => $path, - ]); + return $request->wantsJson() + ? Response::json(['success' => true,'redirect' => $path]) + : Redirect::intended($path); } /** @@ -58,10 +58,8 @@ protected function authenticated(Request $request, $user) */ protected function sendFailedLoginResponse(Request $request) { - return new JsonResponse([ - 'data' => [ - trans('auth.login_invalid_credentials'), - ], - ], 401); + return $request->wantsJson() + ? Response::json(['data' => [trans('auth.login_invalid_credentials')]], 401) + : Redirect::back()->with('message', trans('auth.login_invalid_credentials')); } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 90f20359b..986ce93b5 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -36,7 +36,7 @@ class Kernel extends HttpKernel \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, - \Inertia\Middleware::class, + \App\Http\Middleware\HandleInertiaRequests::class, ], 'api' => [ diff --git a/app/Http/Middleware/HandleInertiaRequests.php b/app/Http/Middleware/HandleInertiaRequests.php new file mode 100644 index 000000000..9ab1e96af --- /dev/null +++ b/app/Http/Middleware/HandleInertiaRequests.php @@ -0,0 +1,73 @@ + fn () => [ + 'user' => $request->user() ? [ + 'id' => $request->user()->id, + 'first_name' => $request->user()->first_name, + 'last_name' => $request->user()->last_name, + 'email' => $request->user()->email, + 'name' => $request->user()->name, + 'show_help' => $request->user()->show_help, + ] : null, + 'company' => $request->user() && ! is_null(InstanceHelper::getLoggedCompany()) ? InstanceHelper::getLoggedCompany() : null, + 'employee' => $request->user() && ! is_null(InstanceHelper::getLoggedEmployee()) ? [ + 'id' => InstanceHelper::getLoggedEmployee()->id, + 'first_name' => InstanceHelper::getLoggedEmployee()->first_name, + 'last_name' => InstanceHelper::getLoggedEmployee()->last_name, + 'name' => InstanceHelper::getLoggedEmployee()->name, + 'permission_level' => InstanceHelper::getLoggedEmployee()->permission_level, + 'display_welcome_message' => InstanceHelper::getLoggedEmployee()->display_welcome_message, + 'user' => (! InstanceHelper::getLoggedEmployee()->user) ? null : [ + 'id' => InstanceHelper::getLoggedEmployee()->user_id, + ], + ] : null, + ], + 'help_links' => fn () => config('officelife.help_links'), + 'flash' => [ + 'message' => fn () => $request->session()->get('message'), + 'success' => fn () => $request->session()->get('success'), + ], + 'errors' => fn () => $request->session()->get('errors') + ? $request->session()->get('errors')->getBag('default')->getMessages() + : [], + ]); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d662f256e..637aed13d 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,14 +2,10 @@ namespace App\Providers; -use Inertia\Inertia; -use App\Helpers\InstanceHelper; use Illuminate\Console\Command; use Illuminate\Support\Facades\App; -use Illuminate\Support\Facades\Auth; use App\Notifications\EmailMessaging; use Illuminate\Support\Facades\Schema; -use Illuminate\Support\Facades\Session; use Illuminate\Support\ServiceProvider; use Illuminate\Auth\Notifications\VerifyEmail; @@ -21,7 +17,6 @@ class AppServiceProvider extends ServiceProvider public function boot(): void { Schema::defaultStringLength(191); - $this->registerInertia(); VerifyEmail::toMailUsing(function ($user, $verificationUrl) { return EmailMessaging::verifyEmailMail($user, $verificationUrl); @@ -49,51 +44,4 @@ public function boot(): void public function register() { } - - public function registerInertia(): void - { - Inertia::version(function () { - return md5_file(public_path('mix-manifest.json')); - }); - - Inertia::share([ - 'auth' => function () { - return [ - 'user' => Auth::user() ? [ - 'id' => Auth::user()->id, - 'first_name' => Auth::user()->first_name, - 'last_name' => Auth::user()->last_name, - 'email' => Auth::user()->email, - 'name' => Auth::user()->name, - 'show_help' => Auth::user()->show_help, - ] : null, - 'company' => Auth::user() && ! is_null(InstanceHelper::getLoggedCompany()) ? InstanceHelper::getLoggedCompany() : null, - 'employee' => Auth::user() && ! is_null(InstanceHelper::getLoggedEmployee()) ? [ - 'id' => InstanceHelper::getLoggedEmployee()->id, - 'first_name' => InstanceHelper::getLoggedEmployee()->first_name, - 'last_name' => InstanceHelper::getLoggedEmployee()->last_name, - 'name' => InstanceHelper::getLoggedEmployee()->name, - 'permission_level' => InstanceHelper::getLoggedEmployee()->permission_level, - 'display_welcome_message' => InstanceHelper::getLoggedEmployee()->display_welcome_message, - 'user' => (! InstanceHelper::getLoggedEmployee()->user) ? null : [ - 'id' => InstanceHelper::getLoggedEmployee()->user_id, - ], - ] : null, - ]; - }, - 'help_links' => function () { - return config('officelife.help_links'); - }, - 'flash' => function () { - return [ - 'success' => Session::get('success'), - ]; - }, - 'errors' => function () { - return Session::get('errors') - ? Session::get('errors')->getBag('default')->getMessages() - : []; - }, - ]); - } } diff --git a/resources/js/Pages/Auth/Login.vue b/resources/js/Pages/Auth/Login.vue index 354b0aff2..46d970115 100644 --- a/resources/js/Pages/Auth/Login.vue +++ b/resources/js/Pages/Auth/Login.vue @@ -20,12 +20,12 @@
- +
{ - this.loadingState = null; - this.$inertia.visit(response.data.redirect); - }) - .catch(error => { - this.loadingState = null; - this.errors = error.response.data; + this.form + .transform(data => ({ + ... data, + remember: this.form.remember ? 'on' : '' + })) + .post(this.route('login'), { + onFinish: () => this.form.reset('password'), }); }, } diff --git a/resources/js/Pages/Error.vue b/resources/js/Pages/Error.vue new file mode 100644 index 000000000..2ca652618 --- /dev/null +++ b/resources/js/Pages/Error.vue @@ -0,0 +1,83 @@ + + + + + diff --git a/resources/js/Shared/Errors.vue b/resources/js/Shared/Errors.vue index d0c2c2a65..7d30c79d9 100644 --- a/resources/js/Shared/Errors.vue +++ b/resources/js/Shared/Errors.vue @@ -8,8 +8,11 @@