我正在查看Drab Live个文档,但仍然无法访问Drab
Commander套接字中的conn分配。我正在使用最新版本:{:drab, "~> 0.9.3"}
。
为什么下面的IO.puts(user)
返回nil
?
Drab Commander:
defmodule ReviewsWeb.PageCommander do
use Drab.Commander
import Ecto.Query
import Ecto.Changeset
import ReviewsWeb.Authorize
import Plug.Conn
defhandler like(socket, sender) do
user = Drab.Core.get_session(socket, :current_user)
IO.puts(user)
IO.puts "Hello World!"
end
end
config.exs
:
config :drab, Reviews.Repo,
otp_app: :reviews
config :drab, enable_live_scripts: true
config :phoenix, :template_engines,
drab: Drab.Live.Engine
config :drab, ReviewsWeb.Endpoint,
otp_app: :reviews,
live_conn_pass_through: %{
assigns: %{
current_user: true
},
private: %{
phoenix_endpoint: true
}
}
IO.inspect(conn)
:
%Plug.Conn{
adapter: {Plug.Adapters.Cowboy.Conn, :...},
assigns: %{
current_user: %{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
__struct__: Reviews.Accounts.User,
avatar: %Reviews.Avatar{
__meta__: #Ecto.Schema.Metadata<:loaded, "avatars">,
id: 74,
image: "1-avatar.jpg",
inserted_at: ~N[2018-10-27 12:41:09.965086],
updated_at: ~N[2018-10-27 12:41:09.965092],
user_id: 1,
users: #Ecto.Association.NotLoaded<association :users is not loaded>
},
答案 0 :(得分:0)
您必须明确[sic]您要访问的会话密钥
:access_session
中的use Drab.Commander
选项或全局config.exs
:config :drab, MyAppWeb.Endpoint, access_session: [:user_id]
因此,您需要在全局配置或:current_user
宏调用中允许您的use
键:
use Drab.Commander, access_session: [:current_user]
还由Drab的创建者在ElixirForum上see the discussion around this。