【问题标题】:Json Authentication to Devise要设计的 Json 身份验证
【发布时间】:2012-06-30 18:38:51
【问题描述】:

我正在尝试在我当前的 rails 应用程序上设置基于 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: some_email@gmail.com}}

错误:

MultiJson::DecodeError (756: unexpected token at '{user: {password: mypass, email:    some_email@gmail.com}}'):
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'

【问题讨论】:

    标签: ruby-on-rails rest devise


    【解决方案1】:

    如果您将该 JSON 粘贴到 http://jsonlint.com 中,您会收到相同的错误。相反,您应该将值用引号括起来:

    {
        "user": {
            "password": "mypass",
            "email": "some_email@gmail.com"
        }
    }
    

    【讨论】:

    • 有点题外话——登录失败时,返回值不是json失败——而是返回html根页面。知道为什么吗?
    • @Newton 我在这里写过类似的文章:jessewolgamott.com/blog/2012/01/19/…
    猜你喜欢
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2020-08-28
    • 2012-02-09
    • 2014-11-15
    • 2022-10-16
    • 1970-01-01
    相关资源
    最近更新 更多