pow-auth / pow_assent

Multi-provider authentication for your Pow enabled app
https://powauth.com
MIT License
321 stars 50 forks source link

Different schema naming #189

Closed gaiabeatrice closed 2 years ago

gaiabeatrice commented 4 years ago

Hello, I am working on a project that already has a user table and I would like to have different namings for the users and user_identities tables.

I thought it was possible because the mix tasks for that accept a new name for the table and the schema modules.

those are the tasks I use:

defmodule MyApp.Admin.DashboardAdminIdentity do
  use Ecto.Schema
  use PowAssent.Ecto.UserIdentities.Schema, user: MyApp.Admin.DashboardAdmin

  schema "dashboard_admin_identities" do
    pow_assent_user_identity_fields()

    timestamps()
  end
end
defmodule MyApp.Admin.DashboardAdmin do
  use Ecto.Schema
  use Pow.Ecto.Schema

  schema "dashboard_admins" do
    has_many :dashboard_admin_identities,
             MyApp.Admin.DashboardAdminIdentity,
             on_delete: :delete_all,
             foreign_key: :user_id

    pow_user_fields()

    timestamps()
  end
end

But it is giving me a lot of problems ** (UndefinedFunctionError) function MyApp.DashboardAdmin.__schema__/2 is undefined (module MyApp.DashboardAdmin is not available) and ** (UndefinedFunctionError) function MyApp.DashboardAdmin.user_identity_changeset/4 is undefined or private.

So my question is: is it possible to actually have different schema and table names without a lot of issues? Is there a guide for it somewhere that can help with this?

Thank you, this is one of the best libraries out there

danschultzer commented 4 years ago

Yeah, you can rename them. This sounds like a config issue. Do you have user: MyApp.Admin.DashboardAdmin in your Pow config?

Thank you, this is one of the best libraries out there

Thanks for your kind words!

gaiabeatrice commented 4 years ago

Good catch, I was missing a word in the user config.

I found in a page that I have to add user_identity_context to the pow_assent config and I did

config :genesis, :pow_assent,
  providers: [
    # list of providers
  ],
  user_identity_context: MyApp.Admin.DashboardAdminIdentity

But I get this error The `:user` configuration option doesn't have a `:user_identities` association. It is not very clear where the user identities association should be defined in this case.

danschultzer commented 4 years ago

Oh right, you need to change the association name from :dashboard_admin_identities to :user_identities:

defmodule MyApp.Admin.DashboardAdmin do
  use Ecto.Schema
  use Pow.Ecto.Schema

  schema "dashboard_admins" do
    has_many :user_identities,
             MyApp.Admin.DashboardAdminIdentity,
             on_delete: :delete_all,
             foreign_key: :user_id

    pow_user_fields()

    timestamps()
  end
end

Then it should be working!

The :user_identity_context is only used for set up custom context module: https://hexdocs.pm/pow_assent/PowAssent.Ecto.UserIdentities.Context.html#content