inertiajs / inertia-rails

The Rails adapter for Inertia.js.
https://inertia-rails.dev/
MIT License
574 stars 45 forks source link

Turn off modals for 401s #27

Closed caseyprovost closed 4 years ago

caseyprovost commented 4 years ago

Using the latest Rails and devise I get the following screen when trying to sign in as an unconfirmed user.

Screen Shot 2020-03-30 at 12 04 01 PM

What I expected to happen is to be redirected back to the sign in page and have that message shown there in a flash message. Or at least be able to customize the controller to do so.

BrandonShar commented 4 years ago

Hmm... this is an interesting case. The reason this is happening is because the inertia front-end interprets every non 2xx status as an error, but doesn't have a special case for 401.

I assume this is happening automatically because of devise? Is it possible to have devise send down the inertia headers ('x-inertia' => true)? If we can do that, inertia's frontend will interpret this like any other inertia response.

If not, we'll have to look into adding a special case for 401 which would be in the https://github.com/inertiajs/inertia repo somewhere around here: https://github.com/inertiajs/inertia/blob/70b6bda4f23061558d48f91a95baaa9062b9e0bb/src/inertia.js#L80

ledermann commented 4 years ago

I encountered a similar problem while creating the PingCRM demo application. Here is my solution:

https://github.com/ledermann/pingcrm/commit/ffd362443fc12967497f28d2472069dd8a06b8e4#diff-d1a22e64e1b54a8e964ea068ea04f8d2

caseyprovost commented 4 years ago

Thanks @ledermann! My next test is to see what happens when the app raises a Pundit::AccessDeniedError.

ledermann commented 4 years ago

@caseyprovost: Here are some more suggestions for exception handling:

Handling AccessDenied exceptions: https://github.com/ledermann/pingcrm/blob/c53d3b40264069bb6dadf63f38b37284f6bd7499/app/controllers/concerns/auth.rb#L9-L13

Handling exceptions in general: https://github.com/ledermann/pingcrm/blob/a0c6dc89d39768397dfe713379d846665051e323/config/application.rb#L34-L41

The idea is to catch the exception and render this Vue component instead: https://github.com/ledermann/pingcrm/blob/master/app/javascript/Pages/Error.vue

caseyprovost commented 4 years ago

Thanks again mate! I’ll definitely be using this 😎👍

caseyprovost commented 4 years ago

@BrandonShar I'm closing this as @ledermann did a great job showing how to make it work :) Might be nice to put some of this in a wiki though or in the Readme :)