Inertia: Opportunity for better 419 handling

Created on 24 Dec 2020  ·  4Comments  ·  Source: inertiajs/inertia

Hi there 👋

The "page expired" solution mentioned in the docs is nice...

You could also use axios response interceptors to have a more seamless experience in the event of a 419 response:

  1. Make a POST request to an endpoint that refreshes the csrf token
  2. Automatically retry the previous inertia request


See example implementation...

Using https://github.com/Flyrell/axios-auth-refresh for a cleaner example...

createAuthRefreshInterceptor(Inertia.axios(), async (failedRequest) => {
  try {
    await Axios.post('/api/refresh-csrf-token')
  } catch (e) {}
}, { statusCodes: [419] })

I'd set this up for my non-inertia requests, but to get it to work for Inertia I had to get the axios instance that was used— solvable by exposing it at e.g. Inertia.axios().

I think that it would be valuable for inertia to cater to this general pattern because:

  • the developer doesn't have to handle a 'page expired' message
  • the user doesn't have to parse the 'page expired' message

But I thought Inertia.axios() getter was a bit 'meh' & also noticed a draft PR to remove axios... so wanted to get your thoughts on the best way forward.

All 4 comments

Hi @jalt007,

We have considered automatically re-submitting the request after a 419 response, but because we're not sure if that would solve or is applicable in every application without weird or unexpected side effects, we (at least for now) opted not to do this and to only reload & re-populate the existing page.

In addition, you're absolutely right that there are serious thoughts about removing Axios altogether to make the library more lightweight (as well as to resolve constant Axios peer dependency conflicts), so I don't think we'll be doing anything any time soon to change this. (cc @reinink: Anything to add/correct?)

In either case, thanks for thinking along & helping us improve Inertia!

I'd set this up for my non-inertia requests, but to get it to work for Inertia I had to get the axios instance that was used— solvable by exposing it at e.g. Inertia.axios().

For what it's worth, people have setup axios interceptors with Inertia. I've never done it, but it certainly seems possible. I think the trick is to make sure that you're app is using the exact same version of axios as Inertia. That will ensure that you don't get two different versions installed...meaning if you configure the interceptor, it will apply to Inertia requests as well. 👍

@claudiodekker Makes sense! Appreciate that stance.

@reinink 💪 Cheers got it working now—made sure app is using same version of axios.

@jalt007 Awesome! 👏

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gajosadrian picture gajosadrian  ·  5Comments

philippkuehn picture philippkuehn  ·  3Comments

bakanyaka picture bakanyaka  ·  5Comments

alighasemzadeh picture alighasemzadeh  ·  4Comments

MichaelDeBoey picture MichaelDeBoey  ·  4Comments