Guardian DB初始设置(MyApp.Repo.insert / 1未定义)

时间:2017-10-05 19:03:28

标签: elixir ecto guardian guardian-db

我试图使用elixir / phoenix的守护者0.14和guardian_db 0.8。但是,我在钩子中继续遇到以下错误:

    [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并返回MyApp.Repo。这就像它没有采取我的repo配置。

1 个答案:

答案 0 :(得分:4)

guardian_db fetches the value of the repo key in the config at compile time,这意味着每当您更改配置中repo的值时,您都需要重新编译该包。这可以通过运行以下命令来完成:

mix deps.clean --build guardian_db