Ruby on rails PhoneGap Mobile Rails认证(从头设计认证?)
我有一个带有Rails后端的PhoneGap应用程序。我正试图找出使用json从移动应用程序验证用户身份的最佳方法 我目前正在使用Desive,但我不必使用它。在Phonegap中修改Desive以使用移动应用程序最简单的方法是什么 我知道有很多关于这个的帖子。。。但是,其中一些已经过时,或者看起来像是非常复杂的黑客。希望有更多的最新信息,从一些尝试和测试的项目,或教程 我发现的一篇帖子也建议使用jsonp,但它似乎也是一个相当复杂的黑客行为。你可以在这里找到它: 我还想知道,如果从零开始进行身份验证,是否会更好,如本Railscast中所述:Ruby on rails PhoneGap Mobile Rails认证(从头设计认证?),ruby-on-rails,ruby,cordova,devise,Ruby On Rails,Ruby,Cordova,Devise,我有一个带有Rails后端的PhoneGap应用程序。我正试图找出使用json从移动应用程序验证用户身份的最佳方法 我目前正在使用Desive,但我不必使用它。在Phonegap中修改Desive以使用移动应用程序最简单的方法是什么 我知道有很多关于这个的帖子。。。但是,其中一些已经过时,或者看起来像是非常复杂的黑客。希望有更多的最新信息,从一些尝试和测试的项目,或教程 我发现的一篇帖子也建议使用jsonp,但它似乎也是一个相当复杂的黑客行为。你可以在这里找到它: 我还想知道,如果从零开始进行身
谢谢 您应该覆盖设备和控制器。我将仅向您展示如何覆盖会话控制器: 首先,转到您的用户模型并添加令牌可验证模块。大概是这样的:
devise :token_authenticatable
before_save :ensure_authentication_token
class SessionsController < Devise::SessionsController
def create
respond_to do |format|
format.html {
super
}
format.json {
build_resource
user = User.find_for_database_authentication(:email => params[:user][:email])
return invalid_login_attempt unless resource
if user.valid_password?(params[:user][:password])
render :json => { :auth_token => user.authentication_token }, success: true, status: :created
else
invalid_login_attempt
end
}
end
end
def destroy
respond_to do |format|
format.html {
super
}
format.json {
user = User.find_by_authentication_token(params[:auth_token])
if user
user.reset_authentication_token!
render :json => { :message => 'Session deleted.' }, :success => true, :status => 204
else
render :json => { :message => 'Invalid token.' }, :status => 404
end
}
end
end
protected
def invalid_login_attempt
warden.custom_failure!
render json: { success: false, message: 'Error with your login or password' }, status: 401
end
end
然后编辑designe.rb文件以配置该模块:
# You can skip storage for :http_auth and :token_auth by adding those symbols to the array below.
config.skip_session_storage = [:token_auth]
# Defines name of the authentication token params key
config.token_authentication_key = :auth_token
现在编辑路线并指向新控制器:
devise_for :users, :controllers => { :registrations => 'registrations', :sessions => 'sessions' }
然后像这样创建控制器:
devise :token_authenticatable
before_save :ensure_authentication_token
class SessionsController < Devise::SessionsController
def create
respond_to do |format|
format.html {
super
}
format.json {
build_resource
user = User.find_for_database_authentication(:email => params[:user][:email])
return invalid_login_attempt unless resource
if user.valid_password?(params[:user][:password])
render :json => { :auth_token => user.authentication_token }, success: true, status: :created
else
invalid_login_attempt
end
}
end
end
def destroy
respond_to do |format|
format.html {
super
}
format.json {
user = User.find_by_authentication_token(params[:auth_token])
if user
user.reset_authentication_token!
render :json => { :message => 'Session deleted.' }, :success => true, :status => 204
else
render :json => { :message => 'Invalid token.' }, :status => 404
end
}
end
end
protected
def invalid_login_attempt
warden.custom_failure!
render json: { success: false, message: 'Error with your login or password' }, status: 401
end
end
class sessioncontrollerparams[:user][:email])
返回无效的\u登录\u尝试,除非资源
if user.valid_password?(参数[:user][:password])
render:json=>{:auth\u token=>user.authentication\u token},成功:true,状态::created
其他的
无效的\u登录\u尝试
结束
}
结束
结束
def销毁
回应待办事项|格式|
format.html{
超级的
}
format.json{
user=user.find\u by\u authentication\u token(params[:auth\u token])
如果用户
user.reset\u身份验证\u令牌!
render:json=>{:message=>'sessiondeleted.},:success=>true,:status=>204
其他的
render:json=>{:message=>'Invalid token'.},:status=>404
结束
}
结束
结束
受保护的
def无效\u登录\u尝试
典狱长,你失败了!
呈现json:{success:false,消息:“登录或密码错误”},状态:401
结束
结束
设计,但它只指向一些已经过时的指南。但也许它会对你有所帮助。+1谢谢你的帮助。您对将会话存储在移动端了解多少?我听说phonegap没有cookie,那么这是否意味着我必须使用localStorage存储cookie,然后以某种方式将其与每个请求一起发布?顺便说一句,Ashitaka,你是对的,那些文档有点过时了。我真的很惊讶这种设置没有更多的文档记录,因为移动应用程序和rails似乎是这些天的趋势。只需搜索本地存储,这非常简单。在Phonegap应用程序中,登录时,对会话url进行ajax调用(您可以使用
rake routes
)然后使用以下内容存储令牌:localStorage.setItem('auth_token',authentication_token)代码>。然后,您可以使用localStorage.getItem('auth_token')
检索它。一个月前,我和一对朋友用Desive和Phonegap做了一个大学项目。你可以看一看,但要谨慎对待。这是我们第一次使用Phonegap,所以它不是一个漂亮的应用程序,但嘿,我们完成了任务。这里是,这里是。希望有帮助!嘿@Ashitaka非常感谢!我会看一眼,然后再看你:)