我在通过 API 登录到我的应用程序时遇到问题。我想通过 POST /api/v1/users/sign_in 对用户进行身份验证,如果带密码的电子邮件有效,则返回此用户的对象。
路线.rb
devise_for :users
...
namespace :api do
namespace :v1 do
devise_scope :user do
post 'users' => 'registrations#create', :as => 'user_registration'
post 'users/sign_in' => 'sessions#create', :as => 'user_session'
end
end
end
控制器/api/v1/sessions_controller.rb
class Api::V1::SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => 'api/v1/sessions#failure')
render :json => resource
end
def failure
render :text => 'not good', :status => 401
end
end
问题是用户从未经过身份验证。它甚至没有从数据库中选择。这是我的本地服务器日志:
Started POST "/api/v1/users/sign_in" for 127.0.0.1 at Tue Jul 12 11:46:55 +0200 2011
SQL (0.6ms) SHOW TABLES
Processing by Api::V1::SessionsController#create as
Parameters: {"user"=>{"password"=>"[FILTERED]", "email"=>"my_email@example.com"}}
Completed in 1ms
Processing by Api::V1::SessionsController#failure as
Parameters: {"user"=>{"password"=>"[FILTERED]", "email"=>"my_email@example.com"}}
Rendered text template (0.0ms)
我已经尝试使用:scope => :api_v1_user
然后提供 POST 数据user_v1_api
,但这并没有帮助。服务器日志看起来与上面的完全一样。
电子邮件和密码绝对正确,因为当我将路径更改/api/vi/users/sign_in
为/users/sign_in
一切正常并且我已登录时。