Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeviseTokenAuth::Errors::InvalidModel #1485

Closed
juhguu opened this issue May 10, 2021 · 1 comment
Closed

DeviseTokenAuth::Errors::InvalidModel #1485

juhguu opened this issue May 10, 2021 · 1 comment

Comments

@juhguu
Copy link

juhguu commented May 10, 2021

Rails version: 6.0.3.7
Devise token auth version: 1.1.5
Ruby Version: 3.0.0

application.rb

require_relative 'boot'

require 'rails'
# Pick the frameworks you want:
require 'active_model/railtie'
require 'active_job/railtie'
require 'active_record/railtie'
require 'active_storage/engine'
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_mailbox/engine'
require 'action_text/engine'
require 'action_view/railtie'
require 'action_cable/engine'
# require "sprockets/railtie"
require 'rails/test_unit/railtie'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module PmeApi
  class Application < Rails::Application
    config.load_defaults 6.0
    config.api_only = true

    config.eager_load_paths << Rails.root.join('lib')

    # Cors Setup
    Rails.application.config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*',
                 headers: :any,
                 expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
                 methods: [:get, :post, :patch, :put, :delete]
      end
    end
  end
end

initializers/devise_token_auth.rb:

# frozen_string_literal: true

DeviseTokenAuth.setup do |config|
  config.change_headers_on_each_request = true
  config.token_lifespan = 2.weeks
  config.token_cost = Rails.env.test? ? 4 : 10
  config.max_number_of_devices = 10
  config.require_client_password_reset_token = true
  config.remove_tokens_after_password_reset = true
end

models/Contributor.rb:

# frozen_string_literal: true

class Contributor < ActiveRecord::Base
  # Devise Settings
  extend Devise::Models
  devise :database_authenticatable
  include DeviseTokenAuth::Concerns::User

  # Validators
  validates :name,
            :email,
            :password,
            presence: true

  validates :email, uniqueness: true
  validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
end

Router file:

# frozen_string_literal: true

Rails.application.routes.draw do
  root to: 'ping#show'

  namespace 'api' do
    mount_devise_token_auth_for 'Contributor', at: 'contributor/auth'

    namespace 'v1' do
      resources :contributors, only: [:create]
    end
  end
end

When I call: [POST] - myapp/api/contributor/auth/sign_in i have this error:

{
"status": 500,
    "error": "Internal Server Error",
    "exception": "#<DeviseTokenAuth::Errors::InvalidModel: Cannot set auth token in invalid model. Errors: [\"Password can't be blank\"]>",
...
}

Request body:

{
    "email": "[email protected]",
    "password": "123456789"
}

what am I doing wrong ?

If I change config.change_headers_on_each_request to false. I have a 200 status request but tokens are not generated.

@juhguu
Copy link
Author

juhguu commented May 11, 2021

OMG. 😂
The error occurred because I was trying to validate the :password column that did not exist in my table. LOL
I just remove the :password validator in my model and all works again. Hahaha

# frozen_string_literal: true

class Contributor < ActiveRecord::Base
  # Devise Settings
  extend Devise::Models
  devise :database_authenticatable
  include DeviseTokenAuth::Concerns::User

  # Validators
  validates :name, :email, presence: true
  validates :email, uniqueness: true
  validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
end

@juhguu juhguu closed this as completed May 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant