diff --git a/lib/pow/ecto/context.ex b/lib/pow/ecto/context.ex index 110c5038..fdf524ef 100644 --- a/lib/pow/ecto/context.ex +++ b/lib/pow/ecto/context.ex @@ -169,7 +169,7 @@ defmodule Pow.Ecto.Context do @spec get_by(Keyword.t() | map(), Config.t()) :: user() | nil def get_by(clauses, config) do user_mod = Config.user!(config) - clauses = normalize_user_id_field_value(user_mod, clauses) + clauses = Keyword.merge(normalize_user_id_field_value(user_mod, clauses), scope_opts(config)) opts = repo_opts(config, [:prefix]) Config.repo!(config).get_by(user_mod, clauses, opts) @@ -192,9 +192,12 @@ defmodule Pow.Ecto.Context do @spec do_insert(changeset(), Config.t()) :: {:ok, user()} | {:error, changeset()} def do_insert(changeset, config) do opts = repo_opts(config, [:prefix]) - repo = Config.repo!(config) + scope = scope_opts(config) - repo.insert(changeset, opts) + changeset + |> Ecto.Changeset.change(scope) + |> Config.repo!(config).insert(opts) + |> reload_after_write(config) end @doc """ @@ -205,9 +208,23 @@ defmodule Pow.Ecto.Context do @spec do_update(changeset(), Config.t()) :: {:ok, user()} | {:error, changeset()} def do_update(changeset, config) do opts = repo_opts(config, [:prefix]) - repo = Config.repo!(config) + scope = scope_opts(config) - repo.update(changeset, opts) + changeset + |> Ecto.Changeset.change(scope) + |> Config.repo!(config).update(opts) + |> reload_after_write(config) + end + + defp reload_after_write({:error, _} = err, _), do: err + + defp reload_after_write({:ok, struct}, config) do + # When ecto updates/inserts, has_many :through associations are set to nil, so need to reload + # when writes happen. + opts = repo_opts(config, [:prefix]) + struct = Config.repo!(config).get!(struct.__struct__, struct.id, opts) + + {:ok, struct} end # TODO: Remove by 1.1.0 @@ -220,6 +237,8 @@ defmodule Pow.Ecto.Context do |> Keyword.take(opts) end + defp scope_opts(config), do: Config.get(config, :scope_opts, []) + # TODO: Remove by 1.1.0 @deprecated "Use `Pow.Config.user!/1` instead" defdelegate user_schema_mod(config), to: Config, as: :user!