我们正在升级一种有两种验证用户身份的方法。
before_filter :authenticate_user!
回调的控制器,可以像标准的Rails站点一样进行访问。/api/session_id/
的身份验证操作外,所有路由都具有前缀/api
。两种方式都使用User
模型对用户进行身份验证。
有没有办法配置设备来支持它们?怎么样?
注意:我不想通过JSON创建用户。我只是想验证它们。
答案 0 :(得分:0)
我通过覆盖devise的会话控制器来做到这一点。
为自定义会话控制器的路由添加条目:
devise_for :users, :controllers => {:sessions => 'sessions'}
覆盖会话控制器:
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "sessions#failure")
return sign_in_and_redirect(resource_name, resource)
end
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
respond_with do |format|
format.json {render :json => {:success => true} }
format.any {super}
end
end
def failure
respond_with do |format|
format.json {render:json => {:success => false, :errors => ["Login failed."]} }
format.any {redirect_to :back, :notice => "Wrong Email / Password" }
end
#return render:json => {:success => false, :errors => ["Login failed."]}
end
end
“Rails & Devise: Override SessionsController是对同一主题的更多讨论。
答案 1 :(得分:0)
结束creating a new stategy to warden。
在valid?
方法上,我检查params[:controller]
是否来自api名称空间。
这样我就没有通过http触及默认的设计认证。