[英]devise_token_auth update request
您好,我正在使用devise_auth_token
gem,并使用邮递员通过以下指示向/
发出PUT请求来测试我的服务器:
帐户更新。 此路由将更新现有用户的帐户设置。 默认接受的参数是password和
password_confirmation
,但是可以使用devise_parameter_sanitizer
系统对其进行自定义。 如果config.check_current_password_before_update
设置为:attributes
,则在任何更新之前检查current_password
参数,如果设置为:password
,则仅在请求更新用户密码时才检查current_password
参数。
我提出了这个要求->
标头:
"access-token": "wwwww",
"token-type": "Bearer",
"client": "xxxxx",
"expiry": "yyyyy",
"uid": "zzzzz"
身体 :
{
"name": "MI NOMBRE",
"nickname": "MI APODO",
"role": "MI ROL"
}
并且我收到此错误: Please submit proper account update data in request body.
这是我在application_controller.rb
允许的参数:
protect_from_forgery with: :null_session
before_filter :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :password_confirmation
devise_parameter_sanitizer.for(:account_update) do |user_params|
user_params.permit(:role, :email, :nickname, :name, :password, :password_confirmation)
end
end
我的伺服器讯息:
Processing by DeviseTokenAuth::RegistrationsController#update as */*
Can't verify CSRF token authenticity
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."uid" = ? LIMIT 1 [["uid", "test2@gmail.com"]]
Filter chain halted as :validate_account_update_params rendered or redirected
Completed 422 Unprocessable Entity in 85ms (Views: 0.2ms | ActiveRecord: 0.5ms)
如果您在本地计算机上对此进行测试,则可能需要先允许跨站点请求。 更新您的Gemfile
并运行bundle install
:
group :development do
gem 'rack-cors'
end
在config/environments/development.rb
添加以下内容:
config.middleware.insert_before 0, Rack::Cors do
# allow all origins in development
allow do
origins '*'
resource '*',
:headers => :any,
:methods => [:get, :post, :delete, :put, :options],
:expose => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
:max_age => 0
end
end
然后在客户端上可以使用多个库。 它们都将auth-token
存储在客户端(cookie,会话存储等),然后将令牌注入每个请求中。 根据您的堆栈,您可以选择:
基本上,验证如下所示( ng-token-auth
图像):
当客户端HTML在服务器(Rails)上呈现时,通常的做法是在页面布局中的某处包括CSRF标签:
<%= csrf_meta_tag %>
如果您真的想用邮递员这样做,并且假设您只有JSON API,则可以在application_controller.rb
设置令牌:
before_action :set_token
def set_token
response.headers['X-XSRF-TOKEN'] = form_authenticity_token
end
现在,当您发送GET /
您将能够检索
X-XSRF-TOKEN: Tjbsg1RYL6bBoCK1u1As8/SO09V+vZ+IOyfNrRXdyfNb9DeWjwnArv6IZkyr2+ayMchwywXPyausYOQhWNGK1g==
来自响应头。 然后,您必须使用它来提交PUT
请求。 只是为了说明它是如何工作的,使用一些库来管理令牌是一个更好的主意。
如果有人正在寻找解决方案,请使用gem'rack-cors'设置跨站点请求。
在application_controller.rb
添加以下内容:
before_action :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :password_confirmation])
devise_parameter_sanitizer.permit(:account_update, keys: [:name, :nickname, :image])
end
经过测试并在Rails 5.2.1中工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.