rails设计简单的身份验证令牌获得403 forbbiden

时间:2016-06-14 05:56:06

标签: ruby-on-rails ruby devise

我使用rails 4.2.6和ruby 2.1.1p76。

我使用带有phone_number的设计作为主要标识符。

我正在尝试为登录创建API服务,但我总是得到403错误禁止。

class Api::V1::SessionsController < Devise::SessionsController
  #skip_before_filter :verify_authenticity_token
  acts_as_token_authentication_handler_for User
  respond_to :json


  def create
    #user = User.find_by phone_number: params['user']['phone_number']
    warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
    if(current_user != nil)

        #current_user.authentication_token = generateToken
            render :status => 200,
               :json => {  :code => 200,
                               :data => {:user => current_user} }
    else
        @new_user = User.new(user_params)
        @new_user.email = 'null@null.com'
        if @new_user.save!
            render :status => 201,
                   :json => {  :code => 201,
                               :data => {:user => @new_user} }
        else
            render :status => 500,
                   :json => {  :code => 500,
                               :data => {} }
        end
    end

  end


  def failure
    render :status => 403,
           :json => { :code => 200,
                      :data => {} }
  end

  def user_params
    params.require(:user).permit(:email,:password,:phone_number)
  end
end

我发送的信息

{"user":{"phone_number":"0535934245","authentication_token":"2zfaWT2QySFigyE9XvsP"}}

希望能帮助你。

最好的注意事项, SHARON DAHAN

1 个答案:

答案 0 :(得分:0)

LOGIN API的CURL示例

curl -v http://xyz/users/sign_in -X POST -H&#34;接受:application / json&#34; -H&#34; Content-Type:application / json&#34; -d&#39; {&#34; user&#34;:{&#34; login&#34;:&#34; 7838712847&#34; ,&#34;密码&#34;:&#34; 8489&#34;}}&#39;

此curl应返回具有身份验证令牌的用户对象。登录后,此身份验证令牌用户将用于与我们的应用程序进行通信

因此,登录后,您需要在每个请求标头中发送用户的身份验证令牌