小编Sam*_*uel的帖子

为什么authenticate_with_http_token不起作用?

我有一个用户控制器,如下所示:

class Api::V1::UsersController < ApiController
  include ActionController::HttpAuthentication::Token::ControllerMethods
  before_action :find_user, only: [:show]
  before_action :authenticate, only: [:destroy]

  ...

  def destroy
    if @user
      @user.destroy
    else
      render json: { user: "not found" }, status: :not_found
    end
  end

  private
  
    ...  

    def authenticate
      authenticate_with_http_token do |token, options|
        @user = User.find_by(token: token)
      end
    end
end
Run Code Online (Sandbox Code Playgroud)

我也有这个测试

describe "DELETE #destroy" do
    before(:each) do
      @user = FactoryBot.create :user
      request.env['HTTP_AUTHORIZATION'] = @user.token
      delete :destroy, params: { id: @user.id }
    end

    it { should respond_with 204 }
  end
Run Code Online (Sandbox Code Playgroud)

每次我运行bundle exec …

ruby authentication ruby-on-rails rspec-rails

6
推荐指数
1
解决办法
3425
查看次数

标签 统计

authentication ×1

rspec-rails ×1

ruby ×1

ruby-on-rails ×1