ran*_*ika 9 ruby-on-rails devise rails-api
如果有人可以解决一些问题,我会非常感激,因为我对这个问题毫无头绪.基本上我正在尝试使用仅在rails-api gem 之上构建的JSON only rails rest api app来完成设计认证.
我已经实现了如下所述的处理Sessions和Registrations处理.
class ApplicationController < ActionController::API
include ActionController::MimeResponds
end
Run Code Online (Sandbox Code Playgroud)
class SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, :only => [:create ]
before_filter :ensure_params_exist
def create
build_resource
resource = User.find_for_database_authentication(:email => params[:user][:email])
return invalid_login_attempt unless resource
if resource.valid_password?(params[:user][:password])
sign_in("user", resource)
render :json=> {:success=>true, :auth_token=>resource.authentication_token, :email=>resource.email}
return
end
invalid_login_attempt
end
def destroy
sign_out(resource_name)
end
protected
def ensure_params_exist
return unless params[:user][:email].blank?
render :json=>{:success=>false, :message=>"missing login email parameter"}, :status=>422
end
def invalid_login_attempt
warden.custom_failure!
render :json=> {:success=>false, :message=>"Error with your login or password"}, :status=>401
end
end
Run Code Online (Sandbox Code Playgroud)
class RegistrationsController < Devise::RegistrationsController
skip_before_filter :verify_authenticity_token, :only => :create
def create
user = User.new(params[:user])
if user.save
render :json=> {:user => [:email => user.email, :auth_token => user.authentication_token]}, :status => 201
return
else
warden.custom_failure!
render :json=> user.errors, :status=>422
end
end
end
Run Code Online (Sandbox Code Playgroud)
到目前为止一切正常.在我的其他控制器中,我添加了这一行来保护它们.before_filter :authenticate_user!但是使用auth_token调用时出现此错误.?AUTH_TOKEN = XXXXXXXXXXX
undefined method authenticate_user!
class RestaurantsController < ApplicationController
before_filter :authenticate_user!
def index
@restaurants = Restaurant.all
end
def show
@restaurant = Restaurant.find(params[:id])
end
end
Run Code Online (Sandbox Code Playgroud)
仍然不确定这里可能存在什么问题 - 我假设这种情况正在发生,因为工作时缺少某些东西,因为它的使用 ActionController::API
更新
似乎问题在于我routes.rb自己 - 这就是路线的完成方式.
require 'api_constraints'
MyApi::Application.routes.draw do
namespace :api, defaults: {format: 'json'} do
scope module: :v1, constraints: ApiConstraints.new(version: 1,default: true) do
devise_for :users
resources :friends
resources :locations
resources :profiles
resources :users
end
scope module: :v2, constraints: ApiConstraints.new(version: 2) do
# Future releases of the API can go here
end
end
end
Run Code Online (Sandbox Code Playgroud)
现在如果重复devise_for :users外部范围,一切都开始工作.
require 'api_constraints'
MyApi::Application.routes.draw do
namespace :api, defaults: {format: 'json'} do
scope module: :v1, constraints: ApiConstraints.new(version: 1,default: true) do
devise_for :users
resources :friends
resources :locations
resources :profiles
resources :users
end
scope module: :v2, constraints: ApiConstraints.new(version: 2) do
# Future releases of the API can go here
end
end
devise_for :users
end
Run Code Online (Sandbox Code Playgroud)
有没有人解释为什么?
步骤1.使用自定义控制器覆盖设计控制器,该控制器将重定向替换为JSON响应.这是一个使用JSON响应的自定义SessionController.
步骤2.如果发生身份验证错误,控件将进入warden,该warden使用的failure_app只是一个Rack应用程序,它根据请求的格式设置flash消息并呈现/重定向.您需要一个自定义的json响应,其中包含一个错误键,而不是默认的错误键.因此,您需要在config/initializers下使用此内容的custom_auth_failure_app.rb .
步骤4.现在我们必须通过将此添加到config/initializers/devise.rb以下内容来告诉Devise使用自定义失败应用程序:
config.warden do |manager|
manager.failure_app = CustomAuthFailure
end
Run Code Online (Sandbox Code Playgroud)
步骤5. token_authenticatable在设计模型中启用并添加以下内容config/initializers/devise.rb
config.http_authenticatable = true
config.skip_session_storage = [:http_auth, :token_auth]
Run Code Online (Sandbox Code Playgroud)
步骤6.如果您使用的是不使用会话的真正JSON API,请使用warden(> 1.2.2)版本,该版本具有处理nil会话的补丁.
另请阅读我的博客文章,了解如何使用Rails4 + Rails-API + Devise创建经过测试,记录和版本化的JSON API,其中讨论了所有这些步骤.
| 归档时间: |
|
| 查看次数: |
9683 次 |
| 最近记录: |