Rails 关于需要先登录的 API,求助。。。。

jjzxcc · May 13, 2013 · Last by mimosa replied at May 13, 2013 · 2562 hits

在用 Grape 写 api,想请问下对于那些需要先登录才能有返回的 api,要怎么写或者说是要怎么测试了呢。 比如 api 文件里有

resource :organization do
   get 'current' do
      Organization.find_by_id(current_user[:organization_id])
   end
end

helper 方法

def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
end

我测这个 current 方法的时候要怎么先登录呢?

求助啊

# 当前用户
  def current_user
    @current_user ||= User.authenticate!(headers['X-Auth-Token'])
  end

  # 认证
  def authenticate!
    error!('401 Unauthorized', 401) unless current_user
  end
You need to Sign in before reply, if you don't have an account, please Sign up first.