使用devise配置simple_token_authentication

时间:2018-01-24 16:45:48

标签: ruby-on-rails devise

设置devise和simple_token_authentication并配置

的ApplicationController

DeviceEventEmitter.addListener('updatePosition', (Data) => {
      console.log(Data);

点击用户编辑个人资料时显示:

  

Devise中的ArgumentError :: RegistrationsController#编辑错误的数字   参数(给定1,预期为0)提取的来源(第20行):

错误指向authenticate_user!

 acts_as_token_authentication_handler_for User
  respond_to :html, :json
  protect_from_forgery with: :null_session

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
  #added username to signup
  def configure_permitted_parameters
    added_attrs = [:name, :username, :email, :password, :remember_me, :avatar, :avatar_cache, :remove_avatar]
    devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
    devise_parameter_sanitizer.permit :account_update, keys: added_attrs

  end


#Override authenticate_user! to handle html and json separately
def authenticate_user!
    if self.request.format.html?
      super
    elsif self.request.format.json?
      if self.request.parameters["controller"].start_with?("devise")
        # use the default if session related
        super
      else
        # others
        if user_signed_in?
          # use the default if already signed in
          super
        else

          redirect_to root_path
        end
      end
    end
  end

在这种情况下,无法覆盖authenticate_user!? 或者这与rails 5.1相关?

有人在使用simple_token_authentication吗?

1 个答案:

答案 0 :(得分:0)

错误消息告诉您Devise定义了一个名为authenticate_user!的方法,它接受一个参数。您正在覆盖该方法,但您的方法不会采用任何参数。在某些时候,Devise打电话authenticate_user!(some_arg_here),所以你得到错误解释:

                         Called with one arg...
                            |
                            V
wrong number of arguments (given 1, expected 0)
                                       ^
                                       |
                             ...but the method def expects 0 args

以下是您的错误的一个简单示例:

class Dog
  def bark
    puts "wuff"
  end
end

Dog.new.bark(3)

--output:--
1.rb:2:in `bark': wrong number of arguments 
(given 1, expected 0) (ArgumentError)

以下是修复:

class Dog
  def bark(repeat)
    puts (["wuff"]*repeat).join(", ")
  end
end

Dog.new.bark(3)

--output:--
wuff, wuff, wuff

因此,您需要定义authenticate_user!方法,以便它需要一个参数。如果您不关心参数,请将其命名为trash