existential-birds/beagle / plugins/beagle-elixir/skills/phoenix-code-review
phoenix-code-review
VerifiedReviews Phoenix code for controller patterns, context boundaries, routing, and plugs. Use when reviewing Phoenix apps, checking controllers, routers, or context modules.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 3.4 KB |
| references/contexts.md | markdown | 2.6 KB |
| references/controllers.md | markdown | 2.6 KB |
| references/plugs.md | markdown | 3.0 KB |
| references/routing.md | markdown | 2.3 KB |
references/plugs.md
View on GitHub# Phoenix Plugs
## Custom Plugs
### Module Plug Structure
```elixir
defmodule MyAppWeb.Plugs.RequireAuth do
import Plug.Conn
import Phoenix.Controller
def init(opts), do: opts
def call(conn, _opts) do
if conn.assigns[:current_user] do
conn
else
conn
|> put_status(:unauthorized)
|> put_view(MyAppWeb.ErrorJSON)
|> render(:"401")
|> halt() # IMPORTANT: halt after sending response
end
end
end
```
### Function Plug
```elixir
defmodule MyAppWeb.UserController do
plug :load_user when action in [:show, :edit, :update]
defp load_user(conn, _opts) do
case Accounts.get_user(conn.params["id"]) do
{:ok, user} -> assign(conn, :user, user)
{:error, :not_found} ->
conn
|> put_status(:not_found)
|> render(:not_found)
|> halt()
end
end
end
```
## Authentication Pattern
```elixir
defmodule MyAppWeb.Plugs.LoadCurrentUser do
import Plug.Conn
def init(opts), do: opts
def call(conn, _opts) do
user_id = get_session(conn, :user_id)
cond do
conn.assigns[:current_user] ->
conn # Already loaded
user_id && user = Accounts.get_user!(user_id) ->
assign(conn, :current_user, user)
true ->
assign(conn, :current_user, nil)
end
end
end
```
## Authorization Pattern
```elixir
defmodule MyAppWeb.Plugs.RequireAdmin do
import Plug.Conn
import Phoenix.Controller
def init(opts), do: opts
def call(conn, _opts) do
user = conn.assigns[:current_user]
if user && user.admin do
conn
else
conn
|> put_status(:forbidden)
|> put_view(MyAppWeb.ErrorJSON)
|> render(:"403")
|> halt()
end
end
end
```
## Plug Composition
```elixir
# In router
pipeline :authenticated do
plug MyAppWeb.Plugs.LoadCurrentUser
plug MyAppWeb.Plugs.RequireAuth
end
pipeline :admin do
plug MyAppWeb.Plugs.RequireAdmin
end
scope "/admin", MyAppWeb.Admin do
pipe_through [:browser, :authenticated, :admin]
# ...
end
```
## Common Mistakes
### Forgetting to Halt
```elixir
# BAD - continues to controller after sending response
def call(conn, _opts) do
if unauthorized?(conn) do
conn
|> send_resp(401, "Unauthorized")
# Missing halt()! Controller still runs
else
conn
end
end
# GOOD
def call(conn, _opts) do
if unauthorized?(conn) do
conn
|> send_resp(401, "Unauthorized")
|> halt()
else
conn
end
end
```
### Modifying Halted Conn
```elixir
# BAD - checking after halt
def call(conn, _opts) do
conn = maybe_halt(conn)
assign(conn, :data, load_data()) # Runs even if halted!
end
# GOOD - check halted status
def call(conn, _opts) do
conn = maybe_halt(conn)
if conn.halted do
conn
else
assign(conn, :data, load_data())
end
end
```
## Review Questions
1. Do plugs call halt() after sending a response?
2. Is authentication handled via plugs, not controller logic?
3. Are plugs composable and single-purpose?
4. Is halted status checked before further processing?
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |