How to add custom/additional fields to users table with pow?

Hi All,
I have implemented Pow authentication for my API, following this documentation. How to use Pow in an API — Pow v1.0.26

However, I need few additional fields into my users table apart from pow_user_fields(). I have updated schema and added additional fields. However, when I try to registration, only pow_user_fields are populated in the users table. Other fields are just populated with null, though I am passing all fields with registration api.

How to get all fields populated in users table?

Thanks,
Rajasekhar.

Can we see the User module please?

Okay, so a few things come to mind without seeing your code.

  1. First you want to create a new migration file with the new fields you want to add mix ecto.gen.migration update_users_table. This sets up your actual database table
  def change do

    alter table(:users) do
      add :field_name, :field_type
    end

  end
  1. Update the schema “users.ex” with the same fields you added to your migration file. This file is just a data model to describe a user from your database.

  2. Now in the same schema you want to update your changeset with cast(data, params, permitted, opts \\ []). You want to pass atoms of the new fields in as a list. For example:

  def changeset(user_or_changeset, attrs) do
    user_or_changeset
    |> cast(attrs, [
      :active,
      :customer_id,
      :first_name,
      :last_name,
      :level,
      :phone_number
    ])
    |> pow_user_id_field_changeset(attrs)
    |> password_changeset(attrs, @pow_config)
    |> validate_required([:customer_id, :first_name, :last_name, :level])
  end
  1. Finally you want to run mix ecto.migrate to migrate the changes you’ve made to the database.
2 Likes

This resolved the issue. Thank you, asnyder002.

Cervajz,
Here is my users module. I was just using pow_changeset.

  schema "users" do
    field :name, :string
    field :mobile, :string
    field :role, :string, default: "user"

    pow_user_fields()

    timestamps()
  end

  def changeset(user_or_changeset, attrs) do
    pow_changeset(user_or_changeset, attrs)
  end

Are you adding these new fields to the registration form? I added a new field to the table, but I want to have the user fill in the value in the form. Were you able to do this?