Skip to content

How to manually generate a valid Inertia request? #241

@AdrienGiboire

Description

@AdrienGiboire

I am trying to setup Rack Attack. And I came up with this bit:

class Rack::Attack
  # [...]

  self.throttled_responder = lambda do |request|
    # If the request is an API request, return JSON
    if request.path == '/users/sessions/verify' && request.env['action_dispatch.request.content_type'].symbol === :json
      [
        429, # status
        { 'Content-Type' => 'application/json' }, # headers
        [{
          error: 'Too many attempts. Please try again later.',
          retry_after: (request.env['rack.attack.match_data'] || {})[:period]
        }.to_json]
      ]
    else
      # For normal web requests, redirect to login page with flash message
      # Store the flash message in the session
      request.session['flash'] = {
        'alert' => 'Too many login attempts. Please try again later.'
      }

      [
        302, # status - redirect
        { 'Location' => '/users/sign_in', 'Content-Type' => 'text/html' }, # headers
        ['Redirecting...'] # body
      ]
    end
  end
end

Inertia complains:

All Inertia requests must receive a valid Inertia response, however a plain JSON response was received.
{"error":"Too many attempts. Please try again later.","retry_after":60}

I started looking at the source but I have not figure out if InertiaRails can help with that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions