Middleware is helpful whenever you want to globally intercept request.
This is accomplished by assigning a MiddlewareStack to your
ApplicationRecord. Each stack has beforeFilters and afterFilters
where you can globally modify requests. If you throw("abort"), the
promise will be rejected.
Example: redirecting to the login page every time the server returns 401:
Example: adding a custom header before the request is sent: