簡體   English   中英

devise_token_auth更新請求

[英]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圖像):

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM