Guardian db不将记录插入数据库

问题描述 投票:0回答:1

我的混音文件包含

{:guardian, "~> 1.0"},
{:guardian_db, "~> 1.1"},

和配置包含

config :my_app, MyApp.Guardian,
   issuer: "my_app",
   ttl: {30, :days},
   allowed_drift: 2000,
   verify_issuer: true,

  #  mix guardian.gen.secret (to get a key for dev and prod envs)
   secret_key: "yKwVGXFyH6nbiE+ELRMLYjCDC3QughF02LN+xPlB7z2loDKeNuBJ6RIUdTMBul23"

config :guardian, Guardian.DB,
   repo: Qserv.BaseRepo,
   schema_name: "sessions", # default
   token_types: ["refresh_token"], # store all token types if not set
   sweep_interval: 60

我的申请有这条线

worker(Guardian.DB.Token.SweeperServer, []),

和我的会话表迁移

defmodule MyApp.Repo.Migrations.CreateTable.Auth.Sessions do
  use Ecto.Migration

  @table :sessions

  def change do
    create table(@table, primary_key: false) do
      add :jti, :string, primary_key: true
      add :aud, :string, primary_key: true
      add :typ, :string
      add :iss, :string
      add :sub, :string
      add :exp, :bigint
      add :jwt, :text
      add :claims, :map
      timestamps()
    end

    create index(@table, [:jwt])
    create index(@table, [:sub])
    create index(@table, [:jti])
  end
end

我有这个文件

defmodule MyApp.Guardian do
  use Guardian, otp_app: :my_app

  def subject_for_token(resource, _claims) do
    sub = to_string(resource.id)
    {:ok, sub}
  end
  def subject_for_token(_, _) do
    {:error, :reason_for_error}
  end

  def resource_from_claims(claims) do
    resource = %{id: 1}
    {:ok,  resource}
  end

  def resource_from_claims(_claims) do
    {:error, :reason_for_error}
  end

  def after_encode_and_sign(resource, claims, token, _options) do
    with {:ok, _} <- Guardian.DB.after_encode_and_sign(resource, claims["typ"], claims, token) do
      {:ok, token}
    else whatever ->
      IO.inspect whatever

    end
  end

  def on_verify(claims, token, _options) do
    with {:ok, _} <- Guardian.DB.on_verify(claims, token) do
      {:ok, claims}
    end
  end

  def on_revoke(claims, token, _options) do
    with {:ok, _} <- Guardian.DB.on_revoke(claims, token) do
      {:ok, claims}
    end
  end
end

一切都很好,我能够成功登录并获得令牌,除了监护人数据库无法将记录插入数据库。我这样登录

MyApp.Guardian.encode_and_sign(%{id:1},%{key :: value},token_type:“cus”)

我也打印了after_encode_and_sign,资源并声称正确的监护人数据库挂钩,但令牌详细信息未插入数据库。这可能是错的

configuration elixir phoenix-framework guardian guardian-db
1个回答
3
投票

在配置中指定了token_types: ["refresh_token"]

通过调用Guardian.encode,您可以创建访问令牌,这就是数据库中没有任何内容的原因。

要保留所有类型的令牌,请删除此行。

© www.soinside.com 2019 - 2024. All rights reserved.