我正在按照插件上的Phoenix 指南创建自己的插件,Module Plug
从会话中加载当前用户。使用@user
Plug Module 时未分配 ,但当我将其作为router.ex
.
这是我的web/router
:
defmodule MyApp.Router do
use MyApp.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug MyApp.Plugs.User
end
# Scopes and Routes...
end
这是我的模块(在web/plugs/user.ex
):
defmodule MyApp.Plugs.User do
import Plug.Conn
def init(default), do: default
def call(conn, default) do
user = %{
id: get_session(conn, :user_id),
username: get_session(conn, :username)
}
assign(conn, :user, user)
IO.inspect conn
end
end
我尝试检查它是否真的被分配,但它不是:
%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{},
before_send: [#Function<1.75757453/1 in Plug.CSRFProtection.call/2>,
#Function<1.30521811/1 in Phoenix.Controller.fetch_flash/2>,
#Function<0.39713025/1 in Plug.Session.before_send/2>,
#Function<1.7608073/1 in Plug.Logger.call/2>,
#Function<0.72065156/1 in Phoenix.LiveReloader.before_send_inject_reloader/1>],
body_params: %{},
cookies: ....