I am new to rails. I am trying to write an API for a mobile application which authenticates using JSON. I want to use Devise for the authentication. I believe Devise >1.3 adds support for JSON as well but I cant find any examples or literature on it.
我是铁杆新手。我正在尝试为使用JSON进行身份验证的移动应用程序编写API。我想使用Devise进行身份验证。我相信Devise> 1.3也增加了对JSON的支持,但我找不到任何关于它的例子或文献。
Can someone please point me to any resource for this or provide some sample code?
有人可以指出我的任何资源或提供一些示例代码?
Thanks! Satyam
谢谢!萨蒂扬
3 个解决方案
#1
6
Perhaps this one > http://jessehowarth.com/devise?
也许这个> http://jessehowarth.com/devise?
I plan to do the same thing in a week or two.
我打算在一两个星期内做同样的事情。
#2
6
I couldn't get to the linked page in the top answer so I thought I would add my own. You don't need to create any custom controllers. All you need to do is the following:
我无法进入顶部答案中的链接页面,所以我想我会添加自己的。您无需创建任何自定义控制器。您需要做的就是以下内容:
In your application.rb add the following in your application class
在application.rb中,在应用程序类中添加以下内容
config.to_prepare do
DeviseController.respond_to :html, :json
end
In config/initializers/devise.rb add :json
to the formats. This line is commented out by default so you will need to uncomment the line.
在config / initializers / devise.rb中添加:json格式。默认情况下,此行已注释掉,因此您需要取消注释该行。
config.navigational_formats = ['*/*', :html, :json]
After this, you can send a json object to sign_in.json or whatever you have set up in your routes for sign-in.
在此之后,您可以将json对象发送到sign_in.json或您在路由中设置的任何用于登录的内容。
{
"user": {
"email": "blah@blah.com",
"password": "blah"
}
}
And on success it will return 201 created, and the logged in user as a JSON object. On error it will return 401 with a JSON message indicating the reason for failure.
并且在成功时它将返回201创建,并将登录用户作为JSON对象。如果出错,它将返回401,并带有指示失败原因的JSON消息。
Example:
例:
{"error":"Invalid email or password."}
Here is a good example if you are using Backbone/Marionntte on the front end.
如果您在前端使用Backbone / Marionntte,这是一个很好的例子。
http://joshhuckabee.com/integrating-devise-backbonejs
http://joshhuckabee.com/integrating-devise-backbonejs
#3
4
The Jesse Howarth's solution will break logging with browser when using html format responses. If you want to make both JSON and HTML to work try someting like this:
使用html格式响应时,Jesse Howarth的解决方案将破坏浏览器的日志记录。如果你想让JSON和HTML都工作,试试这样:
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html { respond_with resource, :location => after_sign_in_path_for(resource) }
format.json {
return render :json => { :success => true,
:user => resource
}
}
end
end
end
And don't forget to change your routes to use this controller
并且不要忘记更改路由以使用此控制器
#1
6
Perhaps this one > http://jessehowarth.com/devise?
也许这个> http://jessehowarth.com/devise?
I plan to do the same thing in a week or two.
我打算在一两个星期内做同样的事情。
#2
6
I couldn't get to the linked page in the top answer so I thought I would add my own. You don't need to create any custom controllers. All you need to do is the following:
我无法进入顶部答案中的链接页面,所以我想我会添加自己的。您无需创建任何自定义控制器。您需要做的就是以下内容:
In your application.rb add the following in your application class
在application.rb中,在应用程序类中添加以下内容
config.to_prepare do
DeviseController.respond_to :html, :json
end
In config/initializers/devise.rb add :json
to the formats. This line is commented out by default so you will need to uncomment the line.
在config / initializers / devise.rb中添加:json格式。默认情况下,此行已注释掉,因此您需要取消注释该行。
config.navigational_formats = ['*/*', :html, :json]
After this, you can send a json object to sign_in.json or whatever you have set up in your routes for sign-in.
在此之后,您可以将json对象发送到sign_in.json或您在路由中设置的任何用于登录的内容。
{
"user": {
"email": "blah@blah.com",
"password": "blah"
}
}
And on success it will return 201 created, and the logged in user as a JSON object. On error it will return 401 with a JSON message indicating the reason for failure.
并且在成功时它将返回201创建,并将登录用户作为JSON对象。如果出错,它将返回401,并带有指示失败原因的JSON消息。
Example:
例:
{"error":"Invalid email or password."}
Here is a good example if you are using Backbone/Marionntte on the front end.
如果您在前端使用Backbone / Marionntte,这是一个很好的例子。
http://joshhuckabee.com/integrating-devise-backbonejs
http://joshhuckabee.com/integrating-devise-backbonejs
#3
4
The Jesse Howarth's solution will break logging with browser when using html format responses. If you want to make both JSON and HTML to work try someting like this:
使用html格式响应时,Jesse Howarth的解决方案将破坏浏览器的日志记录。如果你想让JSON和HTML都工作,试试这样:
class SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html { respond_with resource, :location => after_sign_in_path_for(resource) }
format.json {
return render :json => { :success => true,
:user => resource
}
}
end
end
end
And don't forget to change your routes to use this controller
并且不要忘记更改路由以使用此控制器