Plug pipeline in controllers
In many web frameworks, there's a way to filter and intercept requests that are dispatched to a controller’s action and perform specific checks on them. For example, Rails has filters, specifically the before_action
hooks, which are called before the request reaches the controller’s action. These checks can log a request, perform a redirect, or even halt the request-response cycle. A well-known usage of these filters is to ensure that an incoming request is authenticated:
Figure 5.1 – Plug pipeline in Phoenix
Here’s how authentication using filters looks in a Rails controller:
class PageController < ApplicationController before_action :ensure_authenticated! before_action :ensure_authorized!, only: [:index] def index # index action related code end def show # show action related code...