[英]Devise API sign_in doesn't work with postman
我在 API 模式和最新版本的設計中使用 Rails 6。
我為 sign_in 和 sign_up 創建了 rspec 測試,它可以正常工作,它們接受 json 答案並在 json 中響應
當我嘗試使用郵遞員測試我的 API 時,它適用於 sign_up 但不適用於 sign_in。
與郵遞員一起,我得到了以下回報
{
"sucess": false,
"message": "fail myapp_failure_app"
}
所以我嘗試在devise gem
挖掘問題,我終於發現問題就在這里
def create
self.resource = warden.authenticate!(auth_options)
...
end
在warden gem
,問題發生在方法_run_callbacks(*args)
def _run_callbacks(*args) #:nodoc:
self.class._run_callbacks(*args)
end
傳遞給此方法的參數是:
[:after_set_user, #<User id: 4, nickname: "max", email: "test@test", birth_at: nil, created_at: "2020-01-12 22:44:19", updated_at: "2020-01-13 21:44:57">, Warden::Proxy:70223361236700 @config={:default_scope=>:user, :scope_defaults=>{}, :default_strategies=>{:user=>[:jwt, :rememberable, :database_authenticatable]}, :intercept_401=>false, :failure_app=>MyappFailureApp}, {:scope=>:user, :recall=>"sessions#new", :store=>true, :event=>:authentication}]
如果我將方法更改為此
def _run_callbacks(*args) #:nodoc:
return
self.class._run_callbacks(*args)
end
它運作良好。
我希望自己通過挖掘 gem 並將我的結果與我的規范進行比較來找到解決方案,但老實說,我不明白這種方法的真正作用以及為什么它與我的規范不同。
Devise
在warden
上定義了多個鈎子,通過_run_callbacks
方法調用。 看起來您在其中一個回調掛鈎中失敗了。 鈎子可以在設計源中找到。 此外,您的MyappFailureApp
並沒有真正顯示錯誤,是否可以定義一個更好的錯誤? 就像是 -
class MyappFailureApp < Devise::FailureApp
def respond
http_auth
end
def http_auth
self.status = 401
self.headers["WWW-Authenticate"] = %(Bearer realm=#{Devise.http_authentication_realm.inspect}) if http_auth_header?
self.content_type = 'application/json'
self.response_body = http_auth_body
end
def http_auth_body
{ error: [i18n_message] }.to_json
end
def request_format
:json
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.