【问题标题】:Configuring rails grape api with devise token使用设计令牌配置 Rails 葡萄 api
【发布时间】:2015-02-11 23:02:41
【问题描述】:

我正在尝试使用葡萄 api rails 应用程序中的设计配置令牌生成。由于我拥有当前版本的设计,因此已禁用令牌生成。我有几个问题。首先,当我向会话控制器提交用户名和密码时,它给了我一个错误“ensure_authentication_token”:

undefined method `ensure_authentication_token!' for #<User:0x007f880cca9090>

这很奇怪,因为正如您在下面看到的,我在我的用户模型中定义了它,当我在 rails 控制台中手动创建用户时,它可以正常工作。

这是范围问题还是为什么会发生?

用户模型:

class User < ActiveRecord::Base
  before_save :ensure_authentication_token
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


  def ensure_authentication_token
    if authentication_token.blank?
      self.authentication_token = generate_authentication_token
    end
  end

  private

    def generate_authentication_token
      loop do
        token = Devise.friendly_token
        break token unless User.where(authentication_token: token).first
      end
    end
end

Sessions Grape API 控制器:

module API
  module V1
    class Sessions < Grape::API
      include API::V1::Defaults

      resource :sessions do

        params do
          requires :email, type: String, desc: "Email"
          requires :password, type: String, desc: "Password"
        end

       post do
         email = params[:email]
         password = params[:password]

         if email.nil? or password.nil?
           error!({error_code: 404, error_message: "Invalid Email or Password."},401)
           return
         end

         user = User.where(email: email.downcase).first
         if user.nil?
           error!({error_code: 404, error_message: "Invalid Email or Password."},401)
           return
         end

         if !user.valid_password?(password)
           error!({error_code: 404, error_message: "Invalid Email or Password."},401)
           return
         else
           user.ensure_authentication_token!
           user.save
           {status: 'ok', token: user.authentication_token}.to_json
         end
       end
      end
    end
  end
end

第二个问题是,当我关注this blog时,它说我需要在基础api控制器的defaults.rb中添加以下身份验证检查。当我添加“before do”部分时,即使我输入了正确的凭据,我也会收到拒绝访问错误,并且它甚至不会继续到我上面提到的会话控制器的其余部分。

before do
    error!("401 Unauthorized, 401") unless authenticated
  end

  helpers do
    def warden
      env['warden']
    end

    def authenticated
      return true if warden.authenticated?
      params[:access_token] && @user = User.find_by_authentication_token(params[:access_token])
    end

    def current_user
      warden.user || @user
    end
  end

感谢您提供的任何帮助!

编辑:Phillip 是绝对正确的,其中一个问题是由于 ensure_authentication_token 的 bang 版本与非 banged 版本。删除!从控制器解决了这个问题。另一个问题确实是我添加了“before do”循环。

这非常接近工作,我可以在我的 api 中提供和接收令牌,但是当它连接到 ember 时,它抱怨缺少 csrf 令牌,即使我在我的应用程序中设置了“protect_from_forgery with: :null_session” .rb

【问题讨论】:

  • 你不能在阻止之前调用它,因为它会阻止你首先尝试进行的身份验证 api 调用。
  • 当您定义ensure_authentication_token(bang 与非 bang 方法)时,您是打错字还是抱怨 ensure_authentication_token!

标签: ruby-on-rails ruby api devise grape-api


【解决方案1】:

在您的用户模型中,您定义了一个名为 ensure_authentication_token 的方法。

在您的 Session 控制器中,您调用一个名为 ensure_authentication_token! 的方法。

这些方法不一样:Why are exclamation marks used in Ruby methods?

这会阻止您生成身份验证令牌,这可能解释了“401 Unauthorized, 401”错误。

【讨论】:

  • 感谢您的回复!所以在这种情况下,我可能想要非爆炸/!适用于所有情况的版本,对吧?
  • 任何一个都可以,它们必须相同。由于该方法没有爆炸,所以我不会爆炸。无论如何,您正在将记录保存在控制器的下一行,这就是爆炸对设计的意义。更多信息:ruby-doc.org//gems/docs/d/dcu-devise-1.0.7/Devise/Models/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
相关资源
最近更新 更多