janko / rodauth-rails

Rails integration for Rodauth authentication framework
https://github.com/jeremyevans/rodauth
MIT License
565 stars 40 forks source link

Undefined: Error status #246

Closed kinsomicrote closed 7 months ago

kinsomicrote commented 7 months ago

Hello :wave:

I want to use the gem in an API I'm working on. I followed the setup guide to make use of JSON. I intend to make use of the :email_auth option. So, I have this as my config;

class RodauthMain < RodauthBase
  configure do
    enable :email_auth, :verify_account,:json

    # create_account_type_default "guest"

    before_email_auth_request_route do
      throw_error_status(422, "email", "is required") if param("email").empty?
      throw_error_status(422, "email", "is invalid") unless param("email").match?(/\A[^@\s]+@[^@\s]+\z/)
    end

    # Require account verification before allowing login
    verify_account_set_password? false
    create_account_set_password? false
    force_email_auth? true
  end
end

The RodauthBase looks like this;

class RodauthBase < Rodauth::Rails::Auth
  configure do
    # List of authentication features that are loaded.
    enable :create_account, :json, :verify_account

    db Sequel.postgres(extensions: :activerecord_connection, keep_reference: false)

    before_create_account { account[:type] == account_type }

    # Accept only JSON requests.
    only_json? true
  end

  private

  def account_table_ds
    super.where(type: account_type)
  end

  def account_type
    self.class.configuration_name&.to_s || "main"
  end
end

When I make a request to /email-auth-request, I get the error;

#<Sequel::DatabaseError:\"PG::UndefinedColumn: ERROR:  column \\\"status_id\\\" does not exist\\nLINE 1: ... WHERE ((\\\"type\\\" = 'main') AND (\\\"email\\\" = '') AND (\\\"status_id...\\n                                                             ^\\nHINT:  Perhaps you meant to reference the column \\\"accounts.status\\\".\\n\">"

I don't know where this is coming from. In the migrations generated, no column is called; status_id and no statuses table.

What might I be doing wrong?

janko commented 7 months ago

Unless other specified, Rodauth assumes the status column is called status_id. Because I found the default setup with a separate account_statuses table inconvenient for Rails apps, in rodauth-rails I set account_status_column :status in default configuration and have it be a normal integer column (without a foreign key constraint). You probably removed that line when doing changes to the default configuration.

kinsomicrote commented 7 months ago

Correct! Thanks!