3
我想在我當前的rails應用程序上設置基於json的身份驗證。應用程序的身份驗證目前由devise處理。Json身份驗證到設計
我已經閱讀了幾個關於stackoverflow的問題,但我似乎沒有得到它的工作。
的routes.rb:
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :sessions => 'users/sessions' }
SessionsController:
class Users:: SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => :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
return render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)}
end
def failure
return render:json => {:success => false, :errors => ["Login failed."]}
end
end
不知道在那裏我已經錯了。我想提出的調用是:
JSON:
Content-Type: application/json
Accept: application/json
data: {user: {password: mypass, email: [email protected]}}
的錯誤:
MultiJson::DecodeError (756: unexpected token at '{user: {password: mypass, email: [email protected]}}'):
2012-06-30T18:10:10+00:00 app[web.1]: vendor/bundle/ruby/1.9.1/gems/json-1.6.5/lib/json/common.rb:148:in `parse'
稍微偏離主題 - 當登錄失敗時,返回值不是json失敗 - 而是返回html根頁面。任何想法爲什麼? – Karan
@Newton我在這裏寫了類似的文章:http://jessewolgamott.com/blog/2012/01/19/the-one-with-a-json-api-login-using-devise/ –
真棒,謝謝傑西! – Karan