Using auth_token from request headers instead from POST/PUT parameters with Rails 3 / devise

前端 未结 4 905
长情又很酷
长情又很酷 2021-01-30 18:18

I need to use token based authentication in a Rails 3.1 API in conjunction with the most recent version of devise. No problem so far.

Now I do not want to append my :au

4条回答
  •  梦毁少年i
    2021-01-30 18:30

    I had the same need and came up with this solution:

    class YourController < ApplicationController
      prepend_before_filter :get_api_key
      before_filter :authenticate_user!
    
      private
      def get_api_key
        if api_key = params[:api_key].blank? && request.headers["X-API-KEY"]
          params[:api_key] = api_key
        end
      end
    end
    

    Note I have my devise Devise.token_authentication_key set to api_key.

    config.token_authentication_key = :api_key
    

提交回复
热议问题