nejdetkadir / devise-api

The devise-api gem is a convenient way to add authentication to your Ruby on Rails application using the devise gem. It provides support for access tokens and refresh tokens, which allow you to authenticate API requests and keep the user's session active for a longer period of time on the client side
MIT License
152 stars 22 forks source link

uninitialized constant Devise::Api::Responses::ErrorResponseDecorator (NameError) #27

Closed DerekCrosson closed 1 year ago

DerekCrosson commented 1 year ago

I'm using the instructions in the Readme to override the default response but when I prepend the decorator to the response class I get the following error when I start the rails server:

❯ rails s
=> Booting Puma
=> Rails 7.0.6 application starting in development
=> Run `bin/rails server --help` for more startup options
Exiting
/Users/derek/CodeRepos/project/config/initializers/devise.rb:358:in `<main>': uninitialized constant Devise::Api::Responses::ErrorResponseDecorator (NameError)

Devise::Api::Responses::ErrorResponse.prepend Devise::Api::Responses::ErrorResponseDecorator

This is my lib/devise/api/responses/error_response_decorator.rb:

module Devise::Api::Responses::ErrorResponseDecorator
  def body
    if error_description
      {
        errors: [
          {
            status: "401",
            title: error,
            detail: error_description.join(" "),
            source: { pointer: "/data/attributes/token" }
          }
        ]
      }
    end
  end
end

This is the line causing an issue, in the Devise initialiser added after the config block is closed:

Devise::Api::Responses::ErrorResponse.prepend Devise::Api::Responses::ErrorResponseDecorator
DerekCrosson commented 1 year ago

Fixed by requiring the decorator in the devise initializer: require 'devise/api/responses/error_response_decorator'