Guardian DB 初始设置(MyApp.Repo.insert/1 未定义)
Guardian DB Initial Setup (MyApp.Repo.insert/1 is undefined)
我正在尝试将 guardian 0.14 和 guardian_db 0.8 与 elixir/phoenix 一起使用。但是,我将 运行 保留在挂钩中的以下错误中:
[error] #PID<0.983.0> running Api.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /api/login
** (exit) an exception was raised:
** (UndefinedFunctionError) function MyApp.Repo.insert/1 is undefined (module MyApp.Repo is not available)
MyApp.Repo.insert(#Ecto.Changeset<action: nil, changes: %{claims: %{}}, errors: [jwt: {"is invalid", [type: :string, validation: :cast]}], data: #GuardianDb.Token<>, valid?: false>)
lib/guardian_db.ex:87: GuardianDb.after_encode_and_sign/4
(api) web/helpers/guardian_hooks.ex:12: Api.GuardianHooks.after_encode_and_sign/4
(guardian) lib/guardian.ex:89: Guardian.encode_from_hooked/1
(guardian) lib/guardian/plug.ex:183: Guardian.Plug.api_sign_in/4
(api) web/controllers/user_controller.ex:81: Api.UserController.login_user/2
(api) web/controllers/user_controller.ex:1: Api.UserController.action/2
(api) web/controllers/user_controller.ex:1: Api.UserController.phoenix_controller_pipeline/2
(api) lib/api/endpoint.ex:1: Api.Endpoint.instrument/4
(api) lib/phoenix/router.ex:261: Api.Router.dispatch/2
(api) web/router.ex:1: Api.Router.do_call/2
(api) lib/api/endpoint.ex:1: Api.Endpoint.phoenix_pipeline/1
(api) lib/plug/debugger.ex:123: Api.Endpoint."call (overridable 3)"/2
(api) lib/api/endpoint.ex:1: Api.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/me/source/proj/proj_api/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
我的代码如下:
配置
# Guardian config
config :guardian, Guardian,
issuer: "APIAPP",
ttl: { 30, :days },
allowed_drift: 2000,
verify_issuer: true,
secret_key: "SECRETSECRETSECRET",
serializer: Api.GuardianSerializer,
hooks: Api.GuardianHooks
config :guardian_db, GuardianDb,
repo: Api.Repo,
schema_name: "guardian_tokens"
挂钩
defmodule Api.GuardianHooks do
@moduledoc """
Used for hooks for guardian
"""
use Guardian.Hooks
def after_encode_and_sign(resource, claims, token, _options) do
claims = if (claims == nil), do: %{}, else: claims
with {:ok, _} <- GuardianDb.after_encode_and_sign(resource, token["typ"], claims, token) do
{:ok, token}
end
end
def on_verify(claims, token, _options) do
with {:ok, _} <- GuardianDb.on_verify(claims, token) do
{:ok, claims}
end
end
def on_revoke(claims, token, _options) do
with {:ok, _} <- GuardianDb.on_revoke(claims, token) do
{:ok, claims}
end
end
end
为什么它不使用我在配置中传递给它的 repo?
此外,我在那里放了一个撬子,运行 GuardianDb.repo
和它 returns MyApp.Repo
。好像它没有使用我的回购配置。
guardian_db fetches the value of the repo
key in the config at compile time,这意味着只要您在配置中更改 repo
的值,就需要重新编译该包。这可以通过 运行 以下命令完成:
mix deps.clean --build guardian_db
我正在尝试将 guardian 0.14 和 guardian_db 0.8 与 elixir/phoenix 一起使用。但是,我将 运行 保留在挂钩中的以下错误中:
[error] #PID<0.983.0> running Api.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /api/login
** (exit) an exception was raised:
** (UndefinedFunctionError) function MyApp.Repo.insert/1 is undefined (module MyApp.Repo is not available)
MyApp.Repo.insert(#Ecto.Changeset<action: nil, changes: %{claims: %{}}, errors: [jwt: {"is invalid", [type: :string, validation: :cast]}], data: #GuardianDb.Token<>, valid?: false>)
lib/guardian_db.ex:87: GuardianDb.after_encode_and_sign/4
(api) web/helpers/guardian_hooks.ex:12: Api.GuardianHooks.after_encode_and_sign/4
(guardian) lib/guardian.ex:89: Guardian.encode_from_hooked/1
(guardian) lib/guardian/plug.ex:183: Guardian.Plug.api_sign_in/4
(api) web/controllers/user_controller.ex:81: Api.UserController.login_user/2
(api) web/controllers/user_controller.ex:1: Api.UserController.action/2
(api) web/controllers/user_controller.ex:1: Api.UserController.phoenix_controller_pipeline/2
(api) lib/api/endpoint.ex:1: Api.Endpoint.instrument/4
(api) lib/phoenix/router.ex:261: Api.Router.dispatch/2
(api) web/router.ex:1: Api.Router.do_call/2
(api) lib/api/endpoint.ex:1: Api.Endpoint.phoenix_pipeline/1
(api) lib/plug/debugger.ex:123: Api.Endpoint."call (overridable 3)"/2
(api) lib/api/endpoint.ex:1: Api.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/me/source/proj/proj_api/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
我的代码如下:
配置
# Guardian config
config :guardian, Guardian,
issuer: "APIAPP",
ttl: { 30, :days },
allowed_drift: 2000,
verify_issuer: true,
secret_key: "SECRETSECRETSECRET",
serializer: Api.GuardianSerializer,
hooks: Api.GuardianHooks
config :guardian_db, GuardianDb,
repo: Api.Repo,
schema_name: "guardian_tokens"
挂钩
defmodule Api.GuardianHooks do
@moduledoc """
Used for hooks for guardian
"""
use Guardian.Hooks
def after_encode_and_sign(resource, claims, token, _options) do
claims = if (claims == nil), do: %{}, else: claims
with {:ok, _} <- GuardianDb.after_encode_and_sign(resource, token["typ"], claims, token) do
{:ok, token}
end
end
def on_verify(claims, token, _options) do
with {:ok, _} <- GuardianDb.on_verify(claims, token) do
{:ok, claims}
end
end
def on_revoke(claims, token, _options) do
with {:ok, _} <- GuardianDb.on_revoke(claims, token) do
{:ok, claims}
end
end
end
为什么它不使用我在配置中传递给它的 repo?
此外,我在那里放了一个撬子,运行 GuardianDb.repo
和它 returns MyApp.Repo
。好像它没有使用我的回购配置。
guardian_db fetches the value of the repo
key in the config at compile time,这意味着只要您在配置中更改 repo
的值,就需要重新编译该包。这可以通过 运行 以下命令完成:
mix deps.clean --build guardian_db